Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 0 additions & 23 deletions Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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)
}

Expand All @@ -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
}

Expand All @@ -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)
}

Expand Down Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#if canImport(CloudKit)
package import CloudKit
import Foundation

extension CKRecord {
package func copyWithMockSystemFields(
recordChangeTag: String,
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.recordChangeTag = recordChangeTag
unarchiver.modificationDate = modificationDate
guard let record = Self(coder: unarchiver)
else { fatalError("Could not decode CKRecord.") }
return record
}
}

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:
return super.decodeObject(forKey: key)
}
}
}
#endif
40 changes: 25 additions & 15 deletions Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,22 @@
let clock: AnyClock<Duration>

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] = [:]
var deletedRecords: [(CKRecord.ID, CKRecord.RecordType)] = []
package var rateLimit: Int
package var rateLimitDuration: Duration
fileprivate var requestTimestamps: [AnyClock<Duration>.Instant] = []
mutating func nextRecordChangeTag() -> Int {
lastRecordChangeTag += 1
return lastRecordChangeTag
// NB: CloudKit uses base-36 change tags (0…9, a…z, 10…zz, 100…).
mutating func nextRecordChangeTag() -> String {
defer { changeTagCounter += 1 }
return String(changeTagCounter, radix: 36)
}
mutating func nextModificationDate() -> Date {
defer { modificationDateCounter += 1 }
return Date(timeIntervalSinceReferenceDate: TimeInterval(modificationDateCounter))
}
init(
rateLimit: Int,
Expand Down Expand Up @@ -133,6 +139,7 @@

switch savePolicy {
case .ifServerRecordUnchanged:
let batchModificationDate = state.nextModificationDate()
for recordToSave in recordsToSave {
if let share = recordToSave as? CKShare {
let isSavingRootRecord = recordsToSave.contains(where: {
Expand Down Expand Up @@ -207,9 +214,10 @@
return
}

guard let databaseCopy = recordToSave.copy() as? CKRecord
else { fatalError("Could not copy CKRecord.") }
databaseCopy._recordChangeTag = state.nextRecordChangeTag()
let databaseCopy = recordToSave.copyWithMockSystemFields(
recordChangeTag: state.nextRecordChangeTag(),
modificationDate: batchModificationDate
)

for key in databaseCopy.allKeys() {
guard let assetURL = (databaseCopy[key] as? CKAsset)?.fileURL
Expand All @@ -229,22 +237,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(
Expand Down
28 changes: 20 additions & 8 deletions Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand All @@ -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(
Expand Down Expand Up @@ -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<String?>(nil)
package let _pendingRecordZoneChanges = LockIsolated<
OrderedSet<CKSyncEngine.PendingRecordZoneChange>
>([]
Expand Down Expand Up @@ -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
10 changes: 7 additions & 3 deletions Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "zz",
modificationDate: Date(timeIntervalSinceReferenceDate: 42)
)
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) {
Expand Down
4 changes: 2 additions & 2 deletions Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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: "")
Expand Down Expand Up @@ -144,7 +144,7 @@
id: 1,
id🗓️: 0,
isCompleted: 1,
isCompleted🗓️: 30,
isCompleted🗓️: 60,
priority🗓️: 0,
remindersListID: 1,
remindersListID🗓️: 0,
Expand Down
25 changes: 25 additions & 0 deletions Tests/SQLiteDataTests/CloudKitTests/MockCloudDatabaseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading
Loading