Docs Menu

Docs HomeDevelop ApplicationsAtlas Device SDKs

Convert Between Non-Synced Realms and Synced Realms - .NET SDK

On this page

  • Overview
  • Convert a Non-Synced Realm to a Synced Realm
  • Convert a Synced Realm to a Non-Synced Realm

Realm does not have a direct mechanism to add sync to a non-synced realm, or to permanently stop Sync for a synced realm. However, the .NET SDK does provide a method that enables you to copy a realm file for use with a different configuration. With this method, you can easily duplicate a realm's data, which you can then open with a sync or non-sync configuration. This lets you indirectly add Sync to a non-synced realm, or permanently stop a realm from syncing.

Converting a realm from sync to non-sync (or vice versa), you do the following:

  1. Open the existing realm,

  2. Create a configuration for the new realm,

  3. Call the WriteCopy() method on the existing realm to make a copy to the new realm.

The details of these steps differs depending on which direction then copy is happening. The details are outlined in the sections that follow.

In the following code, we open a non-synced realm, create a new FlexibleSyncConfiguration object and then copy the existing realm to the new realm. We then delete the existing realm and open the new realm.

var existingConfig = new RealmConfiguration("example.realm");
var existingRealm = Realm.GetInstance(existingConfig);
var app = App.Create("my-app-id");
var user = await app.LogInAsync(
Credentials.EmailPassword("email@example.com", "password"));
var syncConfig = new FlexibleSyncConfiguration(user);
existingRealm.WriteCopy(syncConfig);
// You can now delete the nonsynced realm:
Realm.DeleteRealm(existingConfig);
// You can now use the synced realm:
var syncedRealm = Realm.GetInstance(syncConfig);

Note

Partition-Based Sync Only

This method only supports converting between a non-sync realm and Partition-Based Sync. If your app uses Flexible Sync, you must manually iterate through the objects in one realm and copy them into the other realm.

In the following code, we first open a synced realm. To be sure that the most recent data is copied to the non-synced realm, we wait for the data to be synced in both directions. We do this by calling WaitForUploadAsync and WaitForDownloadAsync. Then we call WriteCopy() to copy the data to the non-synced realm. At this point, we can delete the synced realm and start using the non-synced realm.

Note

Partition-Based Sync Only

This method only supports converting between a non-sync realm and Partition-Based Sync. If your app uses Flexible Sync, you must manually iterate through the objects in one realm and copy them into the other realm.

// Open the existing sycned realm
var app = App.Create("my-app-id");
var user = await app.LogInAsync(
Credentials.EmailPassword("email@example.com", "password"));
var syncedConfig = new FlexibleSyncConfiguration(user);
var syncedRealm = await Realm.GetInstanceAsync(syncedConfig);
// When copying a Synced realm, you must ensure
// that there are no pending Sync operations. You do this
// by calling WaitForUploadAsync() and WaitForDownloadAsync()
// methods:
var session = syncedRealm.SyncSession;
await session.WaitForUploadAsync();
await session.WaitForDownloadAsync();
var nonSyncConfig = new RealmConfiguration();
syncedRealm.WriteCopy(nonSyncConfig);
// You can now delete the synced realm:
Realm.DeleteRealm(syncedConfig);
// You can now use the nonsynced realm:
var nonSyncedRealm = Realm.GetInstance(nonSyncConfig);

Note

Data Freshness

Although we wait for the data to be synced between the device and Atlas, we cannot guarantee that other devices are not writing new data to Atlas at the same time. However, since we are converting our synced realm to a non-synced realm and will no longer need the data to be up-to-date with other devices, this shouldn't be an issue.

← Check Upload and Download Progress - .NET SDK