Data Sync

Here's a detailed guide with step-by-step instructions on integrating sync functionality within your app, allowing seamless communication with remote peers.

Sync Start and Stop

Required only once, initiating the sync process automatically connects you with the mesh network. Once connected, you immediately begin receiving updates from publishing remote peers and sending updates to subscribing remote peers.

Starting Sync

To start the sync process:

Swift
Kotlin
JS
Java
C#
C++
Rust


Ensure the sync process remains active throughout your app's lifecycle by starting the sync process (startSync) in the top-most scope of your code.

Stopping Sync

To stop the sync process, call the stopSync function. Once called, all active sync subscriptions end and you disconnect from the mesh.

Swift
Kotlin
JS
Java
C#
C++
Rust



Subscriptions Management

DQL sync subscriptions are supported for devices on version 4.5 or later.

Devices on Ditto versions 4.4 or earlier are supported, however will not sync data by way of DQL subscriptions. For information on syncing with devices on version 4.4 and earlier, see Platform Manual > Legacy API & Language > Data Sync (Legacy).

Manage your sync subscriptions:

  • To get your active subscriptions, call the subscriptions method on the ditto.sync namespace. (Retrieving Subscriptions)
  • To cancel a subscription, call cancel on its subscription object you instantiated when setting up your subscription. (Canceling Subscriptions)
  • To confirm cancelation, call the isCancelled field on the subscription object. (Canceling Subscriptions)


Creating Subscriptions

To register a new sync subscription in your app. For example, the following snippet demonstrates how to establish a subscription to sync updates to documents in the cars collection with a field of color set to the value blue:

Swift
Kotlin
JS
Java
C#
C++
Rust


Sync subscriptions also support argument injection using the :argument syntax in DQL:

Swift
Kotlin
JS
Java
C#
C++
Rust



Retrieving Subscriptions

Retrieve active sync subscriptions by calling the subscriptions method on the ditto.sync namespace:

Swift
Kotlin
JS
Java
C#
C++
Rust



Canceling Subscriptions


Swift
Kotlin
JS
Java
C#
C++
Rust


You can check if a sync subscription is canceled by using the isCancelled field on the subscription object:

Swift
Kotlin
JS
Java
C#
C++
Rust



Attachments Sync

Attachments are synced on demand using the Attachment API methods.

Syncing large documents can significantly impact sync performance:

Be cautious handling very large binary data, a deeply embedded or 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.



Updated 12 Mar 2024
Did this page help you?