From c9ad61e6ad9c91f003b45832d790cd1c064354ca Mon Sep 17 00:00:00 2001 From: Lukas Kubanek Date: Wed, 4 Nov 2026 10:40:38 +0100 Subject: [PATCH 1/5] Add helper for mocking `CKRecord` system fields MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `CKRecord.copyWithMockSystemFields(…)` returns a copy with the given values stored in the real system fields, so they behave like server-assigned values without swizzling. Supports `modificationDate` only for now and is not yet used by `MockCloudDatabase`. --- .../Internal/CKRecord+MockSystemFields.swift | 35 ++++++++ .../CloudKitTests/MockSystemFieldsTests.swift | 87 +++++++++++++++++++ 2 files changed, 122 insertions(+) create mode 100644 Sources/SQLiteData/CloudKit/Internal/CKRecord+MockSystemFields.swift create mode 100644 Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift diff --git a/Sources/SQLiteData/CloudKit/Internal/CKRecord+MockSystemFields.swift b/Sources/SQLiteData/CloudKit/Internal/CKRecord+MockSystemFields.swift new file mode 100644 index 00000000..9ed3b0ea --- /dev/null +++ b/Sources/SQLiteData/CloudKit/Internal/CKRecord+MockSystemFields.swift @@ -0,0 +1,35 @@ +#if canImport(CloudKit) + package import CloudKit + import Foundation + + extension CKRecord { + package func copyWithMockSystemFields(modificationDate: Date) -> Self { + let archiver = NSKeyedArchiver(requiringSecureCoding: true) + self.encode(with: archiver) + let unarchiver: MockSystemFieldsUnarchiver + do { + unarchiver = try MockSystemFieldsUnarchiver(forReadingFrom: archiver.encodedData) + } catch { + fatalError("Could not unarchive CKRecord: \(error)") + } + unarchiver.requiresSecureCoding = true + unarchiver.modificationDate = modificationDate + guard let record = Self(coder: unarchiver) + else { fatalError("Could not decode CKRecord.") } + return record + } + } + + private final class MockSystemFieldsUnarchiver: NSKeyedUnarchiver { + var modificationDate: Date? + + override func decodeObject(forKey key: String) -> Any? { + switch key { + case "RecordMtime": + return self.modificationDate + default: + return super.decodeObject(forKey: key) + } + } + } +#endif diff --git a/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift b/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift new file mode 100644 index 00000000..5f07207c --- /dev/null +++ b/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift @@ -0,0 +1,87 @@ +#if canImport(CloudKit) + import CloudKit + import SQLiteData + import Testing + + @Suite struct MockSystemFieldsTests { + @Test func systemFieldsOverride() { + let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + #expect(record.modificationDate == nil) + + let mocked = record.copyWithMockSystemFields( + modificationDate: Date(timeIntervalSinceReferenceDate: 1) + ) + + #expect(mocked.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) + } + + @Test func systemFieldsOverridePreservesValues() { + let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + record["unencryptedValue"] = 42 + record.encryptedValues["encryptedValue"] = 42 + + let mocked = record.copyWithMockSystemFields( + modificationDate: Date(timeIntervalSinceReferenceDate: 1) + ) + + #expect(mocked["unencryptedValue"] as? Int == 42) + #expect(mocked.encryptedValues["encryptedValue"] as? Int == 42) + } + + @Test func systemFieldsOverrideOnShare() { + let share = CKShare( + rootRecord: CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + ) + + let mocked = share.copyWithMockSystemFields( + modificationDate: Date(timeIntervalSinceReferenceDate: 1) + ) + + #expect(mocked.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) + } + + @Test func subsequentSystemFieldsOverride() { + let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + .copyWithMockSystemFields(modificationDate: Date(timeIntervalSinceReferenceDate: 1)) + + let mocked = record.copyWithMockSystemFields( + modificationDate: Date(timeIntervalSinceReferenceDate: 2) + ) + + #expect(mocked.modificationDate == Date(timeIntervalSinceReferenceDate: 2)) + } + + @Test func copyPreservesMockSystemFields() throws { + let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + .copyWithMockSystemFields(modificationDate: Date(timeIntervalSinceReferenceDate: 1)) + + let copy = try #require(record.copy() as? CKRecord) + + #expect(copy.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) + } + + @Test func systemFieldsRepresentationRoundtrip() throws { + let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + .copyWithMockSystemFields(modificationDate: Date(timeIntervalSinceReferenceDate: 1)) + + let representation = CKRecord.SystemFieldsRepresentation(queryOutput: record) + let result = try #require( + CKRecord.SystemFieldsRepresentation(queryBinding: representation.queryBinding) + ) + + #expect(result.queryOutput.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) + } + + @Test func allFieldsRepresentationRoundtrip() throws { + let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + .copyWithMockSystemFields(modificationDate: Date(timeIntervalSinceReferenceDate: 1)) + + let representation = CKRecord._AllFieldsRepresentation(queryOutput: record) + let result = try #require( + CKRecord._AllFieldsRepresentation(queryBinding: representation.queryBinding) + ) + + #expect(result.queryOutput.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) + } + } +#endif From 7271902f26637edc3a9715e9ca66df1bba831130 Mon Sep 17 00:00:00 2001 From: Lukas Kubanek Date: Wed, 4 Nov 2026 10:53:24 +0100 Subject: [PATCH 2/5] Set `CKRecord.modificationDate` in `MockCloudDatabase` Records saved in a batch now receive an incrementing modification date, mirroring the CloudKit server. The sync engine's modification date checks therefore run in tests as they do in production, which changes an expectation in one merge conflict test. --- .../CloudKit/Internal/MockCloudDatabase.swift | 11 ++++++-- .../CloudKitTests/MergeConflictTests.swift | 4 +-- .../MockCloudDatabaseTests.swift | 25 +++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift b/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift index 6a964bd2..15b92df2 100644 --- a/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift +++ b/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift @@ -14,6 +14,7 @@ package struct State { private var lastRecordChangeTag = 0 + private var modificationDateCounter = 0 package var storage: [CKRecordZone.ID: Zone] = [:] var assets: [AssetID: Data] = [:] var deletedRecords: [(CKRecord.ID, CKRecord.RecordType)] = [] @@ -24,6 +25,10 @@ lastRecordChangeTag += 1 return lastRecordChangeTag } + mutating func nextModificationDate() -> Date { + defer { modificationDateCounter += 1 } + return Date(timeIntervalSinceReferenceDate: TimeInterval(modificationDateCounter)) + } init( rateLimit: Int, rateLimitDuration: Duration @@ -133,6 +138,7 @@ switch savePolicy { case .ifServerRecordUnchanged: + let batchModificationDate = state.nextModificationDate() for recordToSave in recordsToSave { if let share = recordToSave as? CKShare { let isSavingRootRecord = recordsToSave.contains(where: { @@ -207,8 +213,9 @@ return } - guard let databaseCopy = recordToSave.copy() as? CKRecord - else { fatalError("Could not copy CKRecord.") } + let databaseCopy = recordToSave.copyWithMockSystemFields( + modificationDate: batchModificationDate + ) databaseCopy._recordChangeTag = state.nextRecordChangeTag() for key in databaseCopy.allKeys() { diff --git a/Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift b/Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift index b9840148..926c1eb7 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift @@ -15,7 +15,7 @@ @Suite(.taskLocal(CKRecord._$printTimestamps, true)) final class MergeConflictTests: BaseCloudKitTests, @unchecked Sendable { @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) - @Test func merge_clientRecordUpdatedBeforeServerRecord() async throws { + @Test func clientRecordUpdatedBeforeServerRecord() async throws { try await userDatabase.userWrite { db in try db.seed { RemindersList(id: 1, title: "") @@ -144,7 +144,7 @@ id: 1, id🗓️: 0, isCompleted: 1, - isCompleted🗓️: 30, + isCompleted🗓️: 60, priority🗓️: 0, remindersListID: 1, remindersListID🗓️: 0, diff --git a/Tests/SQLiteDataTests/CloudKitTests/MockCloudDatabaseTests.swift b/Tests/SQLiteDataTests/CloudKitTests/MockCloudDatabaseTests.swift index 465163b9..9c0e8979 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/MockCloudDatabaseTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/MockCloudDatabaseTests.swift @@ -705,6 +705,31 @@ #expect(error?.code == .limitExceeded) } + @Test func modificationDateIncreasesOnSubsequentSaves() throws { + let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + + let (results1, _) = try syncEngine.private.database.modifyRecords(saving: [record]) + let saved1 = try #require(try results1[record.recordID]?.get()) + #expect(saved1.modificationDate == Date(timeIntervalSinceReferenceDate: 0)) + + let (results2, _) = try syncEngine.private.database.modifyRecords(saving: [saved1]) + let saved2 = try #require(try results2[record.recordID]?.get()) + #expect(saved2.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) + } + + @Test func modificationDateIsSharedWithinBatch() throws { + let recordA = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + let recordB = CKRecord(recordType: "B", recordID: CKRecord.ID(recordName: "B")) + + let (saveResults, _) = try syncEngine.private.database.modifyRecords( + saving: [recordA, recordB] + ) + let savedA = try #require(try saveResults[recordA.recordID]?.get()) + let savedB = try #require(try saveResults[recordB.recordID]?.get()) + + #expect(savedA.modificationDate == savedB.modificationDate) + } + @Test func records_limitExceeded() async throws { let remindersListRecord = CKRecord( recordType: RemindersList.tableName, From 9b9542f45e2a346dac47fe51afabd3e63cd8557c Mon Sep 17 00:00:00 2001 From: Lukas Kubanek Date: Wed, 4 Nov 2026 11:09:09 +0100 Subject: [PATCH 3/5] =?UTF-8?q?Support=20`recordChangeTag`=20in=20`CKRecor?= =?UTF-8?q?d.copyWithMockSystemFields(=E2=80=A6)`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `MockCloudDatabase` now stores the change tag in the real system field as well, while the sync machinery still reads `_recordChangeTag` for now. --- .../Internal/CKRecord+MockSystemFields.swift | 9 +++++- .../CloudKit/Internal/MockCloudDatabase.swift | 4 ++- .../CloudKitTests/MockSystemFieldsTests.swift | 31 ++++++++++++++++--- 3 files changed, 38 insertions(+), 6 deletions(-) diff --git a/Sources/SQLiteData/CloudKit/Internal/CKRecord+MockSystemFields.swift b/Sources/SQLiteData/CloudKit/Internal/CKRecord+MockSystemFields.swift index 9ed3b0ea..7f97b336 100644 --- a/Sources/SQLiteData/CloudKit/Internal/CKRecord+MockSystemFields.swift +++ b/Sources/SQLiteData/CloudKit/Internal/CKRecord+MockSystemFields.swift @@ -3,7 +3,10 @@ import Foundation extension CKRecord { - package func copyWithMockSystemFields(modificationDate: Date) -> Self { + package func copyWithMockSystemFields( + recordChangeTag: String, + modificationDate: Date + ) -> Self { let archiver = NSKeyedArchiver(requiringSecureCoding: true) self.encode(with: archiver) let unarchiver: MockSystemFieldsUnarchiver @@ -13,6 +16,7 @@ fatalError("Could not unarchive CKRecord: \(error)") } unarchiver.requiresSecureCoding = true + unarchiver.recordChangeTag = recordChangeTag unarchiver.modificationDate = modificationDate guard let record = Self(coder: unarchiver) else { fatalError("Could not decode CKRecord.") } @@ -21,10 +25,13 @@ } private final class MockSystemFieldsUnarchiver: NSKeyedUnarchiver { + var recordChangeTag: String? var modificationDate: Date? override func decodeObject(forKey key: String) -> Any? { switch key { + case "ETag": + return self.recordChangeTag case "RecordMtime": return self.modificationDate default: diff --git a/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift b/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift index 15b92df2..9847842a 100644 --- a/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift +++ b/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift @@ -213,10 +213,12 @@ return } + let recordChangeTag = state.nextRecordChangeTag() let databaseCopy = recordToSave.copyWithMockSystemFields( + recordChangeTag: "\(recordChangeTag)", modificationDate: batchModificationDate ) - databaseCopy._recordChangeTag = state.nextRecordChangeTag() + databaseCopy._recordChangeTag = recordChangeTag for key in databaseCopy.allKeys() { guard let assetURL = (databaseCopy[key] as? CKAsset)?.fileURL diff --git a/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift b/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift index 5f07207c..098efa3d 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift @@ -6,12 +6,15 @@ @Suite struct MockSystemFieldsTests { @Test func systemFieldsOverride() { let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) + #expect(record.recordChangeTag == nil) #expect(record.modificationDate == nil) let mocked = record.copyWithMockSystemFields( + recordChangeTag: "ab", modificationDate: Date(timeIntervalSinceReferenceDate: 1) ) + #expect(mocked.recordChangeTag == "ab") #expect(mocked.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) } @@ -21,6 +24,7 @@ record.encryptedValues["encryptedValue"] = 42 let mocked = record.copyWithMockSystemFields( + recordChangeTag: "ab", modificationDate: Date(timeIntervalSinceReferenceDate: 1) ) @@ -34,53 +38,72 @@ ) let mocked = share.copyWithMockSystemFields( + recordChangeTag: "ab", modificationDate: Date(timeIntervalSinceReferenceDate: 1) ) + #expect(mocked.recordChangeTag == "ab") #expect(mocked.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) } @Test func subsequentSystemFieldsOverride() { let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) - .copyWithMockSystemFields(modificationDate: Date(timeIntervalSinceReferenceDate: 1)) + .copyWithMockSystemFields( + recordChangeTag: "ab", + modificationDate: Date(timeIntervalSinceReferenceDate: 1) + ) let mocked = record.copyWithMockSystemFields( + recordChangeTag: "ac", modificationDate: Date(timeIntervalSinceReferenceDate: 2) ) + #expect(mocked.recordChangeTag == "ac") #expect(mocked.modificationDate == Date(timeIntervalSinceReferenceDate: 2)) } @Test func copyPreservesMockSystemFields() throws { let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) - .copyWithMockSystemFields(modificationDate: Date(timeIntervalSinceReferenceDate: 1)) + .copyWithMockSystemFields( + recordChangeTag: "ab", + modificationDate: Date(timeIntervalSinceReferenceDate: 1) + ) let copy = try #require(record.copy() as? CKRecord) + #expect(copy.recordChangeTag == "ab") #expect(copy.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) } @Test func systemFieldsRepresentationRoundtrip() throws { let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) - .copyWithMockSystemFields(modificationDate: Date(timeIntervalSinceReferenceDate: 1)) + .copyWithMockSystemFields( + recordChangeTag: "ab", + modificationDate: Date(timeIntervalSinceReferenceDate: 1) + ) let representation = CKRecord.SystemFieldsRepresentation(queryOutput: record) let result = try #require( CKRecord.SystemFieldsRepresentation(queryBinding: representation.queryBinding) ) + #expect(result.queryOutput.recordChangeTag == "ab") #expect(result.queryOutput.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) } @Test func allFieldsRepresentationRoundtrip() throws { let record = CKRecord(recordType: "A", recordID: CKRecord.ID(recordName: "A")) - .copyWithMockSystemFields(modificationDate: Date(timeIntervalSinceReferenceDate: 1)) + .copyWithMockSystemFields( + recordChangeTag: "ab", + modificationDate: Date(timeIntervalSinceReferenceDate: 1) + ) let representation = CKRecord._AllFieldsRepresentation(queryOutput: record) let result = try #require( CKRecord._AllFieldsRepresentation(queryBinding: representation.queryBinding) ) + #expect(result.queryOutput.recordChangeTag == "ab") #expect(result.queryOutput.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) } } From 1425ef30c5f8586d62651194ad05dd05b619de56 Mon Sep 17 00:00:00 2001 From: Lukas Kubanek Date: Wed, 4 Nov 2026 11:36:56 +0100 Subject: [PATCH 4/5] Use actual `CKRecord.recordChangeTag` in mock sync machinery `MockCloudDatabase` and `MockSyncEngine` now read `CKRecord.recordChangeTag` from the record's system field instead of the `_recordChangeTag` mock field, which is removed along with its `isTesting` persistence branches. Change tags become strings but remain digit-based for now, to be replaced later. --- .../CloudKit/CloudKit+StructuredQueries.swift | 23 --------------- .../CloudKit/Internal/MockCloudDatabase.swift | 26 ++++++++--------- .../CloudKit/Internal/MockSyncEngine.swift | 28 +++++++++++++------ .../CloudKitTests/CloudKitTests.swift | 10 +++++-- .../NextRecordZoneChangeBatchTests.swift | 2 +- .../Internal/CloudKit+CustomDump.swift | 3 +- 6 files changed, 42 insertions(+), 50 deletions(-) diff --git a/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift b/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift index d2f0726e..3c9f77b4 100644 --- a/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift +++ b/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift @@ -26,9 +26,6 @@ public var queryBinding: QueryBinding { let archiver = NSKeyedArchiver(requiringSecureCoding: true) queryOutput.encodeSystemFields(with: archiver) - if isTesting { - archiver.encode(queryOutput._recordChangeTag, forKey: "_recordChangeTag") - } return archiver.encodedData.queryBinding } @@ -51,11 +48,6 @@ guard let queryOutput = Record(coder: coder) else { throw DecodingError() } - if isTesting { - queryOutput._recordChangeTag = - coder - .decodeObject(of: NSNumber.self, forKey: "_recordChangeTag")?.intValue - } self.init(queryOutput: queryOutput) } @@ -68,9 +60,6 @@ public var queryBinding: QueryBinding { let archiver = NSKeyedArchiver(requiringSecureCoding: true) queryOutput.encode(with: archiver) - if isTesting { - archiver.encode(queryOutput._recordChangeTag, forKey: "_recordChangeTag") - } return archiver.encodedData.queryBinding } @@ -93,11 +82,6 @@ guard let queryOutput = Record(coder: coder) else { throw DecodingError() } - if isTesting { - queryOutput._recordChangeTag = - coder - .decodeObject(of: NSNumber.self, forKey: "_recordChangeTag")?.intValue - } self.init(queryOutput: queryOutput) } @@ -374,13 +358,6 @@ private struct Unbindable: Error {} - extension CKRecord { - package var _recordChangeTag: Int? { - get { self[#function] } - set { self[#function] = newValue } - } - } - extension DataProtocol { fileprivate var sha256: Data { Data(SHA256.hash(data: self)) diff --git a/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift b/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift index 9847842a..f07ed32a 100644 --- a/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift +++ b/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift @@ -21,9 +21,9 @@ package var rateLimit: Int package var rateLimitDuration: Duration fileprivate var requestTimestamps: [AnyClock.Instant] = [] - mutating func nextRecordChangeTag() -> Int { + mutating func nextRecordChangeTag() -> String { lastRecordChangeTag += 1 - return lastRecordChangeTag + return "\(lastRecordChangeTag)" } mutating func nextModificationDate() -> Date { defer { modificationDateCounter += 1 } @@ -213,12 +213,10 @@ return } - let recordChangeTag = state.nextRecordChangeTag() let databaseCopy = recordToSave.copyWithMockSystemFields( - recordChangeTag: "\(recordChangeTag)", + recordChangeTag: state.nextRecordChangeTag(), modificationDate: batchModificationDate ) - databaseCopy._recordChangeTag = recordChangeTag for key in databaseCopy.allKeys() { guard let assetURL = (databaseCopy[key] as? CKAsset)?.fileURL @@ -238,22 +236,24 @@ // If the parent isn't also being saved in this batch. !recordsToSave.contains(where: { $0.recordID == parent.recordID }), // And if the parent is in the database. - let parentRecord = state.storage[parent.recordID.zoneID]?.records[parent.recordID]? - .copy() - as? CKRecord + let parentRecord = state.storage[parent.recordID.zoneID]?.records[parent.recordID] { - parentRecord._recordChangeTag = state.nextRecordChangeTag() - state.storage[parent.recordID.zoneID]?.records[parent.recordID] = parentRecord + let touchedParentRecord = parentRecord.copyWithMockSystemFields( + recordChangeTag: state.nextRecordChangeTag(), + modificationDate: batchModificationDate + ) + state.storage[parent.recordID.zoneID]?.records[parent.recordID] = + touchedParentRecord } } - switch (existingRecord, recordToSave._recordChangeTag) { + switch (existingRecord, recordToSave.recordChangeTag) { case (.some(let existingRecord), .some(let recordToSaveChangeTag)): // We are trying to save a record with a change tag that also already exists in the // DB. If the tags match, we can save the record. Otherwise, we notify the sync engine // that the server record has changed since it was last synced. - if existingRecord._recordChangeTag == recordToSaveChangeTag { - precondition(existingRecord._recordChangeTag != nil) + if existingRecord.recordChangeTag == recordToSaveChangeTag { + precondition(existingRecord.recordChangeTag != nil) saveRecordToDatabase() } else { saveResults[recordToSave.recordID] = .failure( diff --git a/Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift b/Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift index e3f1f804..fe396da3 100644 --- a/Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift @@ -51,11 +51,12 @@ accum += ((state.storage[zoneID]?.records.values).map { Array($0) } ?? []) .map { $0.copy() as! CKRecord } .filter { - precondition( - $0._recordChangeTag != nil, - "Records stored in database should have their 'recordChangeTag' assigned." - ) - return $0._recordChangeTag! > self.state.changeTag.value + guard let changeTag = $0.recordChangeTag else { + fatalError( + "Records stored in database should have their 'recordChangeTag' assigned." + ) + } + return changeTag.isNewerChangeTag(than: self.state.currentChangeTag.value) } } } @@ -73,8 +74,12 @@ guard !modifications.isEmpty || !deletions.isEmpty else { return } - state.changeTag.withValue { changeTag in - changeTag = modifications.compactMap(\._recordChangeTag).max() ?? changeTag + state.currentChangeTag.withValue { currentChangeTag in + for changeTag in modifications.compactMap(\.recordChangeTag) { + if changeTag.isNewerChangeTag(than: currentChangeTag) { + currentChangeTag = changeTag + } + } } await parentSyncEngine.handleEvent( @@ -136,7 +141,7 @@ @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) package final class MockSyncEngineState: CKSyncEngineStateProtocol { - package let changeTag = LockIsolated(0) + package let currentChangeTag = LockIsolated(nil) package let _pendingRecordZoneChanges = LockIsolated< OrderedSet >([] @@ -463,4 +468,11 @@ } } } + + extension String { + package func isNewerChangeTag(than other: String?) -> Bool { + guard let other else { return true } + return self.count != other.count ? self.count > other.count : self > other + } + } #endif diff --git a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift index 219af38b..bbc0ae3b 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift @@ -696,10 +696,14 @@ } try await syncEngine.processPendingRecordZoneChanges(scope: .private) - let record = try syncEngine.private.database.record(for: RemindersList.recordID(for: 1)) + // NB: Manually setting the system fields simulates another device saving a record. + let record = try syncEngine.private.database + .record(for: RemindersList.recordID(for: 1)) + .copyWithMockSystemFields( + recordChangeTag: "9999", + modificationDate: Date(timeIntervalSinceReferenceDate: 9999) + ) record.setValue("Work", forKey: "title", at: now) - // NB: Manually setting '_recordChangeTag' simulates another device saving a record. - record._recordChangeTag = .random(in: 9999 ... .max) try await syncEngine.modifyRecords(scope: .private, saving: [record]).notify() assertQuery(Reminder.all, database: userDatabase.database) { diff --git a/Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift b/Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift index 8a3c2066..e4840d9c 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift @@ -312,7 +312,7 @@ recordType: "remindersLists", parent: nil, share: nil, - recordChangeTag: 3, + recordChangeTag: "3", id: 1, id🗓️: 0, title: "Personal 3", diff --git a/Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift b/Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift index 16d1903b..94a79dab 100644 --- a/Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift +++ b/Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift @@ -56,7 +56,6 @@ } let nonEncryptedKeys = Set(allKeys()) .subtracting(encryptedValues.allKeys()) - .subtracting(["_recordChangeTag"]) var baseChildren = [ ("recordID", recordID as Any), ("recordType", recordType as Any), @@ -64,7 +63,7 @@ ("share", share as Any), ] if Self.printRecordChangeTag { - baseChildren.append(("recordChangeTag", _recordChangeTag as Any)) + baseChildren.append(("recordChangeTag", recordChangeTag as Any)) } return Mirror( self, From c61dc70ef3a2be691448b81984cbc316267c830c Mon Sep 17 00:00:00 2001 From: Lukas Kubanek Date: Wed, 4 Nov 2026 11:49:35 +0100 Subject: [PATCH 5/5] Use base-36 change tags in `MockCloudDatabase` Matches the format CloudKit uses for `CKRecord.recordChangeTag`. --- .../CloudKit/Internal/MockCloudDatabase.swift | 7 ++++--- .../CloudKitTests/CloudKitTests.swift | 4 ++-- .../CloudKitTests/MockSystemFieldsTests.swift | 18 ++++++++++++++++++ .../NextRecordZoneChangeBatchTests.swift | 2 +- 4 files changed, 25 insertions(+), 6 deletions(-) diff --git a/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift b/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift index f07ed32a..2988dbd9 100644 --- a/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift +++ b/Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift @@ -13,7 +13,7 @@ let clock: AnyClock package struct State { - private var lastRecordChangeTag = 0 + private var changeTagCounter = 0 private var modificationDateCounter = 0 package var storage: [CKRecordZone.ID: Zone] = [:] var assets: [AssetID: Data] = [:] @@ -21,9 +21,10 @@ package var rateLimit: Int package var rateLimitDuration: Duration fileprivate var requestTimestamps: [AnyClock.Instant] = [] + // NB: CloudKit uses base-36 change tags (0…9, a…z, 10…zz, 100…). mutating func nextRecordChangeTag() -> String { - lastRecordChangeTag += 1 - return "\(lastRecordChangeTag)" + defer { changeTagCounter += 1 } + return String(changeTagCounter, radix: 36) } mutating func nextModificationDate() -> Date { defer { modificationDateCounter += 1 } diff --git a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift index bbc0ae3b..625aaf49 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift @@ -700,8 +700,8 @@ let record = try syncEngine.private.database .record(for: RemindersList.recordID(for: 1)) .copyWithMockSystemFields( - recordChangeTag: "9999", - modificationDate: Date(timeIntervalSinceReferenceDate: 9999) + recordChangeTag: "zz", + modificationDate: Date(timeIntervalSinceReferenceDate: 42) ) record.setValue("Work", forKey: "title", at: now) try await syncEngine.modifyRecords(scope: .private, saving: [record]).notify() diff --git a/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift b/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift index 098efa3d..0ee75b02 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/MockSystemFieldsTests.swift @@ -106,5 +106,23 @@ #expect(result.queryOutput.recordChangeTag == "ab") #expect(result.queryOutput.modificationDate == Date(timeIntervalSinceReferenceDate: 1)) } + + @Test func isNewerChangeTag() { + #expect("0".isNewerChangeTag(than: nil)) + + #expect(!"0".isNewerChangeTag(than: "0")) + #expect(!"z".isNewerChangeTag(than: "z")) + + #expect("1".isNewerChangeTag(than: "0")) + #expect(!"0".isNewerChangeTag(than: "1")) + #expect("a".isNewerChangeTag(than: "9")) + #expect(!"9".isNewerChangeTag(than: "a")) + #expect("z".isNewerChangeTag(than: "a")) + + #expect("10".isNewerChangeTag(than: "z")) + #expect(!"z".isNewerChangeTag(than: "10")) + #expect("100".isNewerChangeTag(than: "zz")) + #expect(!"zz".isNewerChangeTag(than: "100")) + } } #endif diff --git a/Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift b/Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift index e4840d9c..b5aaac50 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/NextRecordZoneChangeBatchTests.swift @@ -312,7 +312,7 @@ recordType: "remindersLists", parent: nil, share: nil, - recordChangeTag: "3", + recordChangeTag: "2", id: 1, id🗓️: 0, title: "Personal 3",