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 docid\ bihwaat c2quvwcja4h h /#managing lifetime subscriptions /#canceling subscriptions learn the basics of subscriptions data synchronization, or "sync" for short, refers broadly to the practice of ensuring that peers always have the most up to date and accurate information however, data sync goes beyond basic updates and involves a replication process, along with other sophisticated techniques for ensuring data is up to date and consistent across the mesh network replication, in the most basic terms, refers to the process of copying and syncing data across multiple small peers; as in, the multiple storage locations that run locally within an end user physical environment, such as a mobile device to replicate data in ditto, you establish a subscription a subscription acts as a listener that, in conjunction with a replication query , specifies the specific remote events a small peer is interested in receiving data updates for a replication query is essentially a request made by a peer to fetch data matching the query's criteria from other peers in order to sync these replication queries define the criteria and conditions for the associated subscription for more information, see docid\ vh2a6otl4dlnel0a miu8 in "advanced concepts " 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 docid\ yxemkh1com3csuumqcnlf and docid\ aljx b0k4mimi68y7g0oh start data replication within your app's development lifecycle ( /#initiating replication ) instantiate a top level subscription object ( /#creating subscriptions ) if applicable, end the subscription ( /#canceling subscriptions ) initiating replication before you can set up your subscription listener, initiate the replication process in your app initiating replication indicates that you're ready receive updates from remote peers, as well as send updates to subscribing remote peers 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 try! ditto startsync()try { ditto startsync() } catch (e dittoerror) { // handle error }try { ditto startsync() } catch (err) { console error(err) }try { ditto startsync(); } catch(dittoerror e) { // handle error }try { ditto startsync(); } catch (dittoexception ex) { // handle exception }try { ditto start sync(); } catch (const dittoerror \&err) { std cerr << err what(); }ditto try start sync()?; creating subscriptions in the top most scope of your app, following the startsync method called in the previous step, set up a subscription object you must declare your subscription object from the top most scope of your app to ensure access throughout your app otherwise, you cannot modify or cancel your subscription from any part of your code, resulting in difficulty and potential errors when managing the subscription's lifecycle pass your replication query as an argument to find call the subscribe method, and then pass the subsequent actions and processes you want to execute when your criteria are met as an argument once you've set up your subscription, your subscription query is automatically sent to all peers connected to the mesh network if there are any data changes that match your criteria, ditto automatically triggers your subscribe callback function that performs followup actions and processes in your app let subscription = ditto store collection("your collection name") find(query) subscribe()val subscription = ditto store collection("your collection name") find(query) subscribe()const subscription = ditto store collection("your collection name") find(\[query]) subscribe()const subscription = ditto store collection("your collection name") find(\[query]) subscribe()std shared ptr\<ditto subscription> subscription = ditto get store() collection("your collection name") find(\[query]) subscribe();let collection = ditto store() collection("your collection name") unwrap(); const subscription = collection find(\[query]) subscribe(); 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" let carssubscription = ditto store collection("cars") find("color == 'blue'") subscribe()val carssubscription = ditto store collection("cars") find("color == 'blue'") subscribe()const subscription = ditto store collection("cars") find("color == 'blue'") subscribe()var subscription = ditto store collection("cars") find("color == 'blue'") subscribe()std shared ptr\<ditto subscription> subscription = ditto get store() collection("cars") find("color == 'blue'") subscribe();const subscription = ditto store collection("cars") find("color == 'blue'") subscribe(); managing lifetime subscriptions in order to prevent memory leaks, ditto's built in memory management mechanisms automatically cancels active subscriptions that are no longer needed or relevant therefore, if you store the subscription object at a local scope within your code, once ditto executes your subscribe callback function, the subscription object may be removed from memory leading to unexpected behaviors canceling subscriptions to cancel a subscription, call cancel on the subscription object you set up to establish your subscription subscription cancel()subscription cancel()subscription cancel();subscription cancel();subscription cancel();subscription cancel(); for example, continuing with the previous example, the following snippet illustrates canceling the carssubscription carssubscription cancel()carssubscription cancel()carssubscription cancel();carssubscription cancel();std shared ptr\<ditto subscription> carssubscription = ditto get store() collection("cars") find("color == 'blue'") subscribe(); carssubscription cancel();carssubscription cancel();