Platform Manual

Data Sync

This article provides step-by-step instructions for setting up, maintaining, and ending data subscriptions for peer-to-peer asynchronous replication:



Creating Subscriptions

To set up a replication subscription in your app:

Syncing large documents can significantly impact sync performance:

Be cautious handling very large binary data, a deeply embedded document, or a very large document. Carefully consider using attachments instead of storing the data directly within a document object. For more information, see Attachment and Large Binary Files.

1

Start data replication within your app's development lifecycle. (Initiating Replication)

2

Instantiate a top-level subscription object. (Creating Subscriptions)

3

If applicable, end the subscription. (Canceling Subscriptions)

Initiating Replication



To initiate replication, early in your app lifecycle, such as within AppDelegate.application(_:didFinishLaunchingWithOptions:) or Application.onCreate methods, call the startSync method.

You only need to call the following function once.

You must start replication (startSync) in the top-most scope to ensure that as soon as your app starts, it automatically connects with the mesh network and remains active throughout your app's lifecycle.

Otherwise, the peer-to-peer connection may fail, resulting in remote peers becoming unable to send you updates in realtime.

Swift
Kotlin
JS
Java
C#
C++
Rust


Creating Subscriptions


Swift
Kotlin
JS
C#
C++
Rust


For example, the following snippet demonstrates how to establish a carsSubscription to listen for all updates to documents in the "cars" collection with a field of color set to the value "blue":

Swift
Kotlin
JS
Java
C#
C++
Rust


Managing Lifetime Subscriptions



Canceling Subscriptions


Swift
Kotlin
JS
Java
C#
C++
Rust


For example, continuing with the previous example, the following snippet illustrates canceling the carsSubscription:

Swift
Kotlin
JS
Java
C#
C++
Rust