Docs Menu
Docs Home
/ /
Atlas Device SDKs
/ /

Partition-Based Sync - Kotlin SDK

On this page

  • Partition Value
  • Open a Partition-Based Sync Realm
  • Configure a Partition-Based Sync Realm
  • Check Upload & Download Progress for a Sync Session
  • Migrate from Partition-Based Sync to Flexible Sync
  • Updating Client Code After Migration

Partition-Based Sync is an older mode for using Atlas Device Sync with the Realm Kotlin SDK. We recommend using Flexible Sync for new apps. The information on this page is for users who are still using Partition-Based Sync.

Tip

Realm Kotlin SDK v1.9.0 and newer supports the ability to migrate from Partition-Based Sync to Flexible Sync. For more information, refer to: Migrate from Partition-Based Sync to Flexible Sync.

When you select Partition-Based Sync for your backend App configuration, your client implementation must include a partition value. This is the value of the partition key field you select when you configure Partition-Based Sync.

The partition value determines which data the client application can access.

You must provide a partition value when you open a synced realm.

To open a Partition-Based Sync realm, pass a user, a partition, and a set of Realm object schemas to SyncConfiguration.Builder(). Then, pass the configuration to Realm.open() to open an instance of the realm:

val app = App.create(YOUR_APP_ID)
runBlocking {
val user = app.login(Credentials.anonymous())
val config =
SyncConfiguration.Builder(user, PARTITION, setOf(/*realm object models here*/))
// specify name so realm doesn't just use the "default.realm" file for this user
.name(PARTITION)
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration.name}")
realm.close()
}

To adjust specific configuration settings, use the options provided by SyncConfiguration.Builder:

val app = App.create(YOUR_APP_ID)
runBlocking {
val user = app.login(Credentials.anonymous())
val config =
SyncConfiguration.Builder(user, PARTITION, setOf(/*realm object models here*/))
.maxNumberOfActiveVersions(10)
.waitForInitialRemoteData()
.name("realm name")
.build()
val realm = Realm.open(config)
Log.v("Successfully opened realm: ${realm.configuration}")
realm.close()
}

Note

The progressAsFlow() listener in the Kotlin SDK is only currently available for realms using Partition-Based Sync. The Kotlin SDK does not yet support progress listeners for Flexible Sync.

You can monitor the upload and download progress of a sync session. The sync session starts when you open a synced realm. For more information, refer to Manage a Sync Session.

To monitor Sync upload and download progress, call SyncSession.progressAsFlow()

This method returns a Flow of Progress events. Progress provides the total number of transferrable bytes and the remaining bytes to be transferred.

syncSession.progressAsFlow() takes two arguments:

  • A Direction enum that can be set to UPLOAD or DOWNLOAD. This specifies that the progress stream tracks uploads or downloads.

  • A ProgressMode enum that can be set to either:

    • INDEFINITELY: Sets notifications to continue until the callback is unregistered.

    • CURRENT_CHANGES: Sets notifications to continue until only the currently transferable bytes are synced.

val stream = realm.syncSession.progressAsFlow(
Direction.UPLOAD, ProgressMode.CURRENT_CHANGES
)
stream.collect { progress ->
if (progress.transferableBytes == progress.transferredBytes) {
Log.i("Upload complete")
}
}

You can migrate your App Services Device Sync Mode from Partition-Based Sync to Flexible Sync. Migrating is an automatic process that does not require any changes to your application code. Automatic migration requires Realm Kotlin SDK version 1.9.0 or newer.

Migrating enables you to keep your existing App Services users and authentication configuration. Flexible Sync provides more versatile permissions configuration options and more granular data synchronization.

For more information about how to migrate your App Services App from Partition-Based Sync to Flexible Sync, refer to Migrate Device Sync Modes.

The automatic migration from Partition-Based Sync to Flexible Sync does not require any changes to your client code. However, to support this functionality, Realm automatically handles the differences between the two Sync Modes by:

  • Automatically creating Flexible Sync subscriptions for each object type where partitionKey == partitionValue.

  • Injecting a partitionKey field into every object if one does not already exist. This is required for the automatic Flexible Sync subscription.

If you need to make updates to your client code after migration, consider updating your client codebase to remove hidden migration functionality. You might want update your client codebase when:

  • You add a new model or change a model in your client codebase

  • You add or change functionality that involves reading or writing Realm objects

  • You want to implement more fine-grained control over what data you sync

Make these changes to convert your Partition-Based Sync client code to use Flexible Sync:

  • Update your SyncConfiguration.Builder() to use Flexible Sync. This involves removing the partitionValue and adding a set of initial subscriptions, if needed.

  • Add relevant properties to your object models to use in your Flexible Sync subscriptions. For example, you might add an ownerId property to enable a user to sync only their own data.

  • Remove automatic Flexible Sync subscriptions and manually create the relevant subscriptions.

For examples of Flexible Sync permissions strategies, including examples of how to model data for these strategies, refer to Device Sync Permissions Guide.

When you migrate from Partition-Based Sync to Flexible Sync, Realm automatically creates hidden Flexible Sync subscriptions for your app. The next time you add or change subscriptions, we recommend that you:

  1. Remove the automatically-generated subscriptions.

  2. Subscribe to queries or Manually add the relevant subscriptions in your client codebase.

This enables you to see all of your subscription logic together in your codebase for future iteration and debugging.

For more information about the automatically-generated Flexible Sync subscriptions, refer to Migrate Client App to Flexible Sync.

Back

Sync Data in the Background

Next

Logging