Contents

nextRecordZoneChangeBatch(_:syncEngine:)

Asks the delegate to provide the next set of record changes to send to the server.

Declaration

func nextRecordZoneChangeBatch(_ context: CKSyncEngine.SendChangesContext, syncEngine: CKSyncEngine) async -> CKSyncEngine.RecordZoneChangeBatch?

Parameters

  • context:

    The reason for the sync engine’s request, and any additional options that request is using.

  • syncEngine:

    The sync engine asking about pending changes.

Return Value

If there are pending record changes, a batch of those changes for the sync engine to process; otherwise, nil to indicate there are no changes to send.

Discussion

In your implementation, ask the sync engine’s state for any pending record zone changes and then return a change batch containing a CKRecord instance for each record identifier the state provides, as the following example shows:

func nextRecordZoneChangeBatch(
    _ context: CKSyncEngine.SendChangesContext,
    syncEngine: CKSyncEngine
) async -> CKSyncEngine.RecordZoneChangeBatch? {

    // Get the pending record changes and filter by the context's scope.
    let pendingChanges = syncEngine.state.pendingRecordZoneChanges
        .filter { context.options.zoneIDs.contains($0) }

    // Return a change batch that contains the corresponding materialized records.
    return await CKSyncEngine.RecordZoneChangeBatch(
        pendingChanges: pendingChanges) { self.recordFor(id: $0) }
}

When syncing, you must make sure to only return a batch for the scope specified in the callback. You can do this by checking the scope property in options. If you do not do this, you may encounter a invalidArguments error.

For both scheduled and manual send operations, the sync engine calls this method repeatedly until your app has no more changes and returns nil.

See Also

Sending changes