'Hello World' Sync
Once you've set up your environment, try on Ditto and perform your first peer-to-peer sync.
Intended to get you oriented, this article provides a streamlined process for integrating sync functionality within your app.
If you prefer building and exploring through a chat app, and so on, see Example Projects Overview.
Integrating Sync Functionality
Integrate the Ditto SDK in your codebase, set up authentication, and then start syncing offline:
From the top-most scope of your app's codebase, add the following to set up authentication and start syncing offline.
DittoLogger.minimumLogLevel = .debug
let ditto = Ditto(identity: .onlinePlayground(
appID: "REPLACE_ME_WITH_YOUR_APP_ID",
token: "REPLACE_ME_WITH_YOUR_PLAYGROUND_TOKEN"
))
do {
try ditto.startSync()
} catch (let err) {
print(err.localizedDescription)
}Replace YOUR_APP_ID and YOUR_PLAYGROUND_TOKEN with your access credentials available from the portal.
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:
ditto.sync.registerSubscription("SELECT * FROM cars")Sync subscriptions also support argument injection using the :argument syntax in DQL:
ditto.sync.registerSubscription(
"SELECT * FROM cars WHERE color = :color",
Collections.singletonMap("color", "blue"))Retrieving Subscriptions
Retrieve active sync subscriptions by calling the subscriptions method on the ditto.sync namespace:
const activeSubscriptions = ditto.sync.subscriptionsCanceling Subscriptions
subscription.close()You can check if a sync subscription is canceled by using the isCancelled field on the subscription object:
subscription.isCancelled