问题
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:
fileURLinMemoryIdentifiersyncConfiguration
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