iOS Swift Realm Sync Issue

折月煮酒 提交于 2019-12-10 19:33:00

问题


I used this configuration code for local storage.

var configuration = Realm.Configuration.defaultConfiguration
configuration.encryptionKey = getKey() as Data

I used this configuration code for sync with server.

let syncServerURL = URL(string: serverUrl + objectName!)!
var configuration = Realm.Configuration.defaultConfiguration
configuration.encryptionKey = getKey() as Data     configuration.syncConfiguration = SyncConfiguration(user: SyncUser.current!, realmURL: syncServerURL)

I create some data without sync, it is saved locally. However, if I turn on sync(different configuration), the previously created data(locally) is not synced to the server? How to sync already saved data?


回答1:


Realms are uniquely referenced by one of three mutually exclusive Realm.Configuration properties:

  1. fileURL
  2. inMemoryIdentifier
  3. syncConfiguration

Realm configurations with any of these properties with different values will refer to separate Realms.

So your initial Realm has a fileURL value (with nil for the other two properties), whereas your second Realm has a syncConfiguration value (with nil for the other two properties) so they refer to separate Realms.

If you wish to copy data from the first (local) Realm to the second (synced) Realm, you may do so using Realm's APIs for reading objects & creating objects just like you would for any other data.



来源:https://stackoverflow.com/questions/44324038/ios-swift-realm-sync-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!