Mobile Sync

One of the newest additions in MongoDB Stitch Mobile Sync can be used to sync data seamlessly between MongoDB Mobile and the server's backend (at the moment of writing this, it has to be hosted on MongoDB Atlas). Mobile Sync is also based on change streams to listen to data changing between local and remote databases. As data changes in the local Mobile database, we may run into conflicts between the local and remote state. This is why we need to define some handlers to specify what should happen in that case. There are three interfaces that we need to implement for our models:

  • ConflictHandler
  • ErrorListener
  • ChangeEventListener

ConflictHandler has a method that arguments documentId of the document that conflicts the local and remote events, returns a resolution to the conflict as shown:

DocumentT resolveConflict(BsonValue documentId,
ChangeEvent<DocumentT> localEvent,
ChangeEvent<DocumentT> remoteEvent)

ErrorListener does not return anything and is called when an error happens for documentId and non-network related exception:

void onError(BsonValue documentId,Exception error)

Finally, ChangeEventListener also does not return any values and is called when any change event happens for the given documentId:

void onEvent(BsonValue documentId, ChangeEvent<DocumentT> event)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset