Realm Sync and Local Cohabitation

Hello there, I’m working on a .NET 5 application and I’m using Realm (from nuget) 10.4.0 which is the latest version available as of today. The doc is very clear and there is no bug.

We’re designing a local oriented application (it is intended to run on a Windows Machine without internet most of the time).
Sometimes when the user has internet, he can click on a button to Sync the local .realm file.
I need to access the .realm file and I don’t need to get data from the Synced realm, I just need to upload data from the Local to the Sync.

Currently I’m using a RealmConfiguration locally and when the user press the button I’m launching a function to create a SyncConfiguration and copy everything from the local realm to the synced realm. (Trying to reproduce this schema: Add Sync to a Local-Only App — MongoDB Realm

However when trying to add an object to Synced Realm the object from the Local Realm it throw an exception (Cannot start to manage an object with a realm when it’s already managed by another realm)

  • I don’t know how to be sure that the object I’m adding from the local to the sync will update the current one (if it exists in Sync) or create a copy (which is a problem)

So I’m asking if myself I truly understand the Local to Sync concept, and if I get something wrong can you please help me.

Thank you very much

Hi @Arthur_Junges,

The guide that you linked actually is trying to do something different than what you’re trying to do. The guide is about how to move from a local realm to a synced realm and just use the latter one. Regarding your error, that happens because you cannot move one object from one realm to another one, the only thing you can do is to copy each object one by one, copying all the properties.
Regarding how to be sure that you are updating an object. What you’ll need to do in this case is be sure that you have a PrimaryKey defined on the model, and then use realm.Add with update parameter set to true. In this case if an object with the same primary key is found, then it will be just updated.

Nevertheless, can you maybe explain a little bit better what you’re trying to accomplish? It’s generally not a good idea to have both a local and a synced realm working with the same data. If you really want to have the data to be synchronised only when you want, then you could just use a synced realm and pause and resume the syncing when needed (https://docs.mongodb.com/realm/sdk/dotnet/examples/sync-changes-between-devices/#std-label-dotnet-pause-or-resume-a-sync-session).

Let me know if you need additional help

1 Like

To expand on @papafe’s reply - the main selling point of Realm Sync is that the user/developer shouldn’t have to do anything manual to synchronize with the server. You just read/write from the local database and Sync will pull/push data in the background. I guess wording is a bit tricky here as both local and synchronized Realms store the data locally on the device. With synchronized Realms, the only difference is that there’s a background thread that uploads/downloads changes from the server and resolves conflicts if necessary.

2 Likes