Skip to main content

ModelAdapter

An object conforming to ModelAdapter will track the local model, provide changes to upload to CloudKit and import downloaded changes.

@objc public protocol ModelAdapter: class 

Inheritance#

class

Requirements#

hasChanges#

Whether the model has any changes

var hasChanges: Bool 

prepareToImport()#

Tells the model adapter that an import operation will begin

func prepareToImport()

saveChanges(in:​)#

Apply changes in the provided record to the local model objects and save the records.

func saveChanges(in records: [CKRecord])

Parameters#

  • records: Array of CKRecord that were obtained from CloudKit.

deleteRecords(with:​)#

Delete the local model objects corresponding to the given record IDs.

func deleteRecords(with recordIDs: [CKRecord.ID])

Parameters#

  • recordIDs: Array of identifiers of records that were deleted on CloudKit.

persistImportedChanges(completion:​)#

Tells the model adapter to persist all downloaded changes in the current import operation.

func persistImportedChanges(completion: @escaping (Error?)->())

Parameters#

  • completion: Block to be called after changes have been persisted.

recordsToUpload(limit:​)#

Provides an array of up to limit records with changes that need to be uploaded to CloudKit.

func recordsToUpload(limit: Int) -> [CKRecord]

Parameters#

  • limit: Maximum number of records that should be provided.

Returns#

Array of CKRecord.

didUpload(savedRecords:​)#

Tells the model adapter that these records were uploaded successfully to CloudKit.

func didUpload(savedRecords: [CKRecord])

Parameters#

  • savedRecords: Records that were saved.

recordIDsMarkedForDeletion(limit:​)#

Provides an array of record IDs to be deleted on CloudKit, for model objects that were deleted locally.

func recordIDsMarkedForDeletion(limit: Int) -> [CKRecord.ID]

Parameters#

  • limit: Maximum number of records that should be provided.

Returns#

Array of CKRecordID.

didDelete(recordIDs:​)#

Tells the model adapter that these record identifiers were deleted successfully from CloudKit.

func didDelete(recordIDs: [CKRecord.ID])

Parameters#

  • recordIDs: Record IDs that were deleted on CloudKit.

hasRecordID(_:​)#

Asks the model adapter whether it has a local object for the given record identifier.

func hasRecordID(_ recordID: CKRecord.ID) -> Bool

Parameters#

  • recordID: Record identifier.

Returns#

Whether there is a corresponding object for this identifier.

didFinishImport(with:​)#

Tells the model adapter that the current import operation finished.

func didFinishImport(with error: Error?)

Parameters#

  • error: Optional error, if any error happened.

recordZoneID#

Record zone ID managed by this adapter

var recordZoneID: CKRecordZone.ID 

serverChangeToken#

Latest CKServerChangeToken stored by this adapter, or nil if one does not exist.

var serverChangeToken: CKServerChangeToken? 

saveToken(_:​)#

Save given token for future use by this adapter.

func saveToken(_ token: CKServerChangeToken?)

Parameters#

  • token: CKServerChangeToken

deleteChangeTracking()#

func deleteChangeTracking()
  • Deletes all tracking information and detaches from local model.
  • This adapter should not be used after calling this method, create a new adapter if you wish to synchronize
  • the same model again.

mergePolicy#

Merge policy in case of conflicts. Default is server.

var mergePolicy: MergePolicy 

record(for:​)#

Returns corresponding CKRecord for the given model object.

func record(for object: AnyObject) -> CKRecord?

Parameters#

  • object: Model object.

share(for:​)#

Returns CKShare for the given model object, if one exists.

@available(iOS 10.0, OSX 10.12, *) func share(for object: AnyObject) -> CKShare?

Parameters#

  • object: Model object.

save(share:​for:​)#

Store CKShare for given model object.

@available(iOS 10.0, OSX 10.12, *) func save(share: CKShare, for object: AnyObject)

Parameters#

  • share: CKShare object to save.
  • object: Model object.

deleteShare(for:​)#

Delete existing CKShare for given model object.

@available(iOS 10.0, OSX 10.12, *) func deleteShare(for object: AnyObject)

Parameters#

  • object: Model object.

shareForRecordZone()#

@available(iOS 15.0, OSX 12, *) func shareForRecordZone() -> CKShare?
  • Returns: CKShare for the adapter's record zone, if one exists.

saveShareForRecordZone(share:​)#

Store CKShare for the record zone.

@available(iOS 15.0, OSX 12, *) func saveShareForRecordZone(share: CKShare)

Parameters#

  • share: CKShare object to save.

deleteShareForRecordZone()#

Delete existing CKShare for adapter's record zone.

@available(iOS 15.0, OSX 12, *) func deleteShareForRecordZone()

recordsToUpdateParentRelationshipsForRoot(_:​)#

Returns a list of records for the given object and any parent records, recursively.

func recordsToUpdateParentRelationshipsForRoot(_ object: AnyObject) -> [CKRecord]

Parameters#

  • object: Model object.

Returns#

Array of CKRecord