Syncing synced realm back to local Realm?


#1

I have an offline first application. So far was able to successfully migrate the local Realm to synced Realm by manually copying all my objects, when the user logs in the first time. But when the user logs out, the local Realm is in the state if was before migrating to the synced Realm, meaning changes while the user was logged in are lost. How to I save the data “back” to the local Realm? I feel that I’m missing something fundamental here. Shouldn’t the local Realm automatically be updated?

Additional maybe relevant infos:

  • My local Realm is stored in a shared folder, such that it can be shared with a SiriKit extension (FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "mygroupidentifier"))

  • When the app starts I set the default Realm path to synced / local, depending of whether the user is logged in:

if let user = SyncUser.current {
    Realm.Configuration.defaultConfiguration = RealmConfig.syncedRealmConfig(user: user)
} else {
    Realm.Configuration.defaultConfiguration = RealmConfig.localRealmConfig
}

// Where:

public static func syncedRealmConfig(user: SyncUser) -> Realm.Configuration {
    var config = Realm.Configuration()
    config.syncConfiguration = SyncConfiguration(user: user, realmURL: syncServerURL)
    config.objectTypes = [
        ...
    ]
    return config
}

public static var localRealmConfig: Realm.Configuration {
    var config = Realm.Configuration(
        schemaVersion: RealmConfig.schemaVersion,
        migrationBlock: { migration, oldSchemaVersion in }
    )
    config.fileURL = localRealmUrl
    return config
}

Also, is there somewhere a detailed documentation about how these migrations / paths work? When I implemented this I had to look it up in the demos. It was not documented.