apple / sample-cloudkit-sync-engine

MIT License
172 stars 13 forks source link

How to determine from which array to fetch data for upload? #12

Closed nookery closed 5 months ago

nookery commented 7 months ago

Assuming that two sets of data are stored locally

both of which are kept in sync with CloudKit. How to determine from which array to fetch data for upload at nextRecordZoneChangeBatch?

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

        Logger.database.info("Returning next record change batch for context: \(context)")

        let scope = context.options.scope
        let changes = syncEngine.state.pendingRecordZoneChanges.filter { scope.contains($0) }
        let contacts = self.appData.contacts

        let batch = await CKSyncEngine.RecordZoneChangeBatch(pendingChanges: changes) { recordID in

// ====== How to determine from which array to fetch data for upload? Contacts or orders? ======

            if let contact = contacts[recordID.recordName] {
                let record = contact.lastKnownRecord ?? CKRecord(recordType: Contact.recordType, recordID: recordID)
                contact.populateRecord(record)
                return record
            } else {
                // We might have pending changes that no longer exist in our database. We can remove those from the state.
                syncEngine.state.remove(pendingRecordZoneChanges: [ .saveRecord(recordID) ])
                return nil
            }
        }
        return batch
    }
malhal commented 5 months ago

As a quick fix you could just search both arrays.

nookery commented 5 months ago

As a quick fix you could just search both arrays.

Yes, I have ultimately decided to proceed this way. Thanks.