Syncing synced realm back to local Realm?

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.

Hi Ivan,
Just curious, were you able to figure this out?

If you want to keep the data from the synced Realm when logged out you have to copy the synced data back to the local Realm.

What is the use case that you need this for?

I’m introducing Realm Platform along with authentication to my app soon. So far users have only used a local Realm database.

When they first login, I plan on copying over the local database to the synced realm (which I’m having trouble with but that’s another issue).

Now, what if one of the users decides to logout and switch to a different account? What happens to the local database in this case? Does Realm automatically handle this by downloading the Synced realm or do I have to do this manually?

Let me know if I’m not making sense.

If you have accounts I’d only use the synced realms. When singing in for the first time, copy whatever data you have stored locally into the synced realm. Then you are done. You don’t need the local realm anymore.

When switching users, realm will automatically download the synced realm for the new logged in user and it won’t see the old users data. If you have accounts in your app, I don’t see the point of keeping the local realm when signed out.

This assumes that you are using full synchronization and not query based sync. If you are using query based sync (which is deprecated but still default for some weird reason), then you have more work. But you should be using full synchronization anyway.

If I only use synced realms, how is my app supposed to offer offline mode? Say the user is offline and starts messing with the database, changes need to be persisted locally. Then, when the user connects to the internet, changes should somehow be reflected in the synced realm.

This is exactly what Realm sync was designed for. All data is still there when offline (unless you actively sign out), and will resume syncing when you go back online :wink:

That’s what I thought but I got confused after having to manually upload my local database to Sync Realm. I didn’t think I’d have to do that.

Do you happen to know what is the mechanism (roughly) behind the data being available offline? Is the local realm automatically used when offline?

I am not a Realm employee, but my understanding is that sync is a layer on top of the database (which is local). When you open a Realm using a sync configuration, it will add extra classes and info to the Realm file to make sync possible.

I do think that Realm could improve here as it is not trivial to move data from a local to synced Realm, especially if it contains relational data.

Thanks Sipe. Appreciate your help.