Sync
public class Sync<DocumentT> where DocumentT : Decodable, DocumentT : Encodable
Sync
is a set of synchronization-related operations for a RemoteMongoCollection
.
Using Sync, you can synchronize local and remote data.
See also
Build a Mobile App with Sync, RemoteMongoCollection-
Set the conflict resolver and and change event listener on this collection.
Declaration
Swift
public func configure( conflictHandler: @escaping ( _ documentId: BSONValue, _ localEvent: ChangeEvent<DocumentT>, _ remoteEvent: ChangeEvent<DocumentT>) throws -> DocumentT?, changeEventDelegate: ((_ documentId: BSONValue, _ event: ChangeEvent<DocumentT>) -> Void)? = nil, errorListener: ((_ error: DataSynchronizerError, _ documentId: BSONValue?) -> Void)? = nil, _ completionHandler: @escaping (StitchResult<Void>) -> Void)
Parameters
conflictHandler
the conflict resolver to invoke when a conflict happens between local and remote events.
changeEventDelegate
the event listener to invoke when a change event happens for the document.
errorListener
the error listener to invoke when an irrecoverable error occurs
completionHandler
the handler to execute when configuration is complete
-
Set the conflict resolver and and change event listener on this collection.
Declaration
Swift
public func configure<CH: ConflictHandler, CED: ChangeEventDelegate>( conflictHandler: CH, changeEventDelegate: CED? = nil, errorListener: ErrorListener? = nil, _ completionHandler: @escaping (StitchResult<Void>) -> Void ) where CH.DocumentT == DocumentT, CED.DocumentT == DocumentT
Parameters
conflictHandler
the conflict resolver to invoke when a conflict happens between local and remote events.
changeEventDelegate
the event listener to invoke when a change event happens for the document.
errorListener
the error listener to invoke when an irrecoverable error occurs
completionHandler
the handler to execute when configuration is complete
-
Requests that the given document _ids be synchronized.
Declaration
Swift
public func sync(ids: [BSONValue], _ completionHandler: @escaping (StitchResult<Void>) -> Void)
Parameters
ids
the document _ids to synchronize.
completionHandler
the handler to execute when the provided ids are marked as synced. The documents will not necessarily exist in the local collection yet, but will get synced down in the next background sync pass
-
Stops synchronizing the given document _ids. Any uncommitted writes will be lost.
Declaration
Swift
public func desync(ids: [BSONValue], _ completionHandler: @escaping (StitchResult<Void>) -> Void)
Parameters
ids
the _ids of the documents to desynchronize.
completionHandler
the handler to execute when the provided ids are no longer marked as synced. The documents will be deleted from the local collection, but not the remote collection.
-
Returns the set of synchronized document ids in a namespace. Remove custom AnyBSONValue after: https://jira.mongodb.org/browse/SWIFT-255
Declaration
Swift
public func syncedIds(_ completionHandler: @escaping (StitchResult<Set<AnyBSONValue>>) -> Void)
Return Value
the set of synchronized document ids in a namespace.
-
Return the set of synchronized document _ids in a namespace that have been paused due to an irrecoverable error.
Declaration
Swift
public func pausedIds(_ completionHandler: @escaping (StitchResult<Set<AnyBSONValue>>) -> Void)
Return Value
the set of paused document _ids in a namespace
-
A document that is paused no longer has remote updates applied to it. Any local updates to this document cause it to be resumed. An example of pausing a document is when a conflict is being resolved for that document and the handler throws an exception.
Declaration
Swift
public func resumeSync( forDocumentId documentId: BSONValue, _ completionHandler: @escaping (StitchResult<Bool>) -> Void)
Parameters
documentId
the id of the document to resume syncing
Return Value
true if successfully resumed, false if the document could not be found or there was an error resuming
-
Counts the number of documents in the collection that have been synchronized with the remote.
Declaration
Swift
func count(_ completionHandler: @escaping (StitchResult<Int>) -> Void)
Return Value
the number of documents in the collection
-
Counts the number of documents in the collection that have been synchronized with the remote according to the given options.
Declaration
Swift
func count(filter: Document, options: SyncCountOptions?, _ completionHandler: @escaping (StitchResult<Int>) -> Void)
Parameters
filter
the query filter
options
the options describing the count
completionHandler
the callback for the count result
Return Value
the number of documents in the collection
-
Finds all documents in the collection that have been synchronized with the remote.
Declaration
Swift
func find(_ completionHandler: @escaping (StitchResult<MongoCursor<DocumentT>>) -> Void)
Parameters
completionHandler
the callback for the find result
Return Value
the find iterable interface
-
Finds all documents in the collection that have been synchronized with the remote.
Declaration
Swift
func find( filter: Document, options: SyncFindOptions? = nil, _ completionHandler: @escaping (StitchResult<MongoCursor<DocumentT>>) -> Void)
Parameters
filter
the query filter for this find op
options
the options for this find op
completionHandler
the callback for the find result
Return Value
the find iterable interface
-
Finds a document in the collection that has been synchronized with the remote.
Declaration
Swift
func findOne( filter: Document? = nil, options: SyncFindOptions? = nil, _ completionHandler: @escaping (StitchResult<DocumentT?>) -> Void)
Parameters
filter
the query filter for this find op
options
the options for this find op
completionHandler
the callback for the find result
Return Value
the document or nil if no such document existss
-
Aggregates documents that have been synchronized with the remote according to the specified aggregation pipeline.
Declaration
Swift
func aggregate(pipeline: [Document], options: AggregateOptions?, _ completionHandler: @escaping (StitchResult<MongoCursor<Document>>) -> Void)
Parameters
pipeline
the aggregation pipeline
options
the options for this aggregate op
Return Value
an iterable containing the result of the aggregation operation
-
Inserts the provided document. If the document is missing an identifier, the client should generate one. Syncs the newly inserted document against the remote.
Declaration
Swift
func insertOne(document: DocumentT, _ completionHandler: @escaping (StitchResult<SyncInsertOneResult?>) -> Void)
Parameters
document
the document to insert
Return Value
the result of the insert one operation
-
Inserts one or more documents. Syncs the newly inserted documents against the remote.
Declaration
Swift
func insertMany(documents: [DocumentT], _ completionHandler: @escaping (StitchResult<SyncInsertManyResult?>) -> Void)
Parameters
documents
the documents to insert
Return Value
the result of the insert many operation
-
Removes at most one document from the collection that has been synchronized with the remote that matches the given filter. If no documents match, the collection is not modified.
Declaration
Swift
func deleteOne(filter: Document, _ completionHandler: @escaping (StitchResult<SyncDeleteResult?>) -> Void)
Parameters
filter
the query filter to apply the the delete operation
Return Value
the result of the remove one operation
-
Removes all documents from the collection that have been synchronized with the remote that match the given query filter. If no documents match, the collection is not modified.
Declaration
Swift
func deleteMany(filter: Document, _ completionHandler: @escaping (StitchResult<SyncDeleteResult?>) -> Void)
Parameters
filter
the query filter to apply the the delete operation
Return Value
the result of the remove many operation
-
Update a single document in the collection that have been synchronized with the remote according to the specified arguments. If the update results in an upsert, the newly upserted document will automatically become synchronized.
Declaration
Swift
func updateOne(filter: Document, update: Document, options: SyncUpdateOptions?, _ completionHandler: @escaping (StitchResult<SyncUpdateResult?>) -> Void)
Parameters
filter
a document describing the query filter, which may not be null.
update
a document describing the update, which may not be null. The update to apply must include only update operators.
Return Value
the result of the update one operation
-
Update all documents in the collection that have been synchronized with the remote according to the specified arguments. If the update results in an upsert, the newly upserted document will automatically become synchronized.
Declaration
Swift
func updateMany(filter: Document, update: Document, options: SyncUpdateOptions?, _ completionHandler: @escaping (StitchResult<SyncUpdateResult?>) -> Void)
Parameters
filter
a document describing the query filter, which may not be null.
update
a document describing the update, which may not be null. The update to apply must include only update operators.
updateOptions
the options to apply to the update operation
Return Value
the result of the update many operation