Migrating non-Sync data to a Synced Realm

I have an application built using Realm fully on-device. I am now working on switching to Realm Sync, but haven’t been able to find a good way to preserve the on-device data. From what I understand there are no migrations for Realm Sync, which makes it difficult to do something like populate an owner_id field for use as a partition value. Can anyone recommend a good way to automatically populate all the data on device so as to make it available in the user’s realm?

1 Like

@Peter_Stakoun So sync realms and non-synced realms are different. Synced realms contain a history which is all the operations the user performed in order to generate the current state - preserving the intent of this history is necessary to guard the semantics of our conflict resolution. If you are looking to migrate from non-synced to synced you will need to have code that copies the objects from non-synced to a synced realm.

1 Like

Thanks for the clarification @Ian_Ward. Would I then have to open both the non-synced and synced realms at the same time?

Something like nonSyncedRealm.objects('Obj').forEach((o) => syncedRealm.create('Obj', o))?

Is there a way to check if the synced realm is being opened for the first time?

@Peter_Stakoun Correct, you would open both realms at the same time, providing them different Configuration structs, and copy data between them. Generally, the way to tell if the synced realm is the first time is to see if app.currentUser is null or not - because if you have a valid user then you have logged in before and likely opened a synced realm.

1 Like