Skip to content
Merged
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
20 changes: 20 additions & 0 deletions Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
queryOutput.encodeSystemFields(with: archiver)
if isTesting {
archiver.encode(queryOutput._recordChangeTag, forKey: "_recordChangeTag")
archiver.encode(queryOutput._modificationDate, forKey: "_modificationDate")
if queryOutput is CKShare {
archiver.encode(queryOutput[CKShare.SystemFieldKey.title], forKey: CKShare.SystemFieldKey.title)
}
Expand Down Expand Up @@ -58,6 +59,9 @@
queryOutput._recordChangeTag =
coder
.decodeObject(of: NSNumber.self, forKey: "_recordChangeTag")?.intValue
queryOutput._modificationDate =
coder
.decodeObject(of: NSDate.self, forKey: "_modificationDate") as Date?
if queryOutput is CKShare {
queryOutput[CKShare.SystemFieldKey.title] = coder
.decodeObject(of: NSString.self, forKey: CKShare.SystemFieldKey.title)
Expand All @@ -77,6 +81,10 @@
queryOutput.encode(with: archiver)
if isTesting {
archiver.encode(queryOutput._recordChangeTag, forKey: "_recordChangeTag")
archiver.encode(queryOutput._modificationDate, forKey: "_modificationDate")
if queryOutput is CKShare {
archiver.encode(queryOutput[CKShare.SystemFieldKey.title], forKey: CKShare.SystemFieldKey.title)
}
}
return archiver.encodedData.queryBinding
}
Expand Down Expand Up @@ -104,6 +112,13 @@
queryOutput._recordChangeTag =
coder
.decodeObject(of: NSNumber.self, forKey: "_recordChangeTag")?.intValue
queryOutput._modificationDate =
coder
.decodeObject(of: NSDate.self, forKey: "_modificationDate") as Date?
if queryOutput is CKShare {
queryOutput[CKShare.SystemFieldKey.title] = coder
.decodeObject(of: NSString.self, forKey: CKShare.SystemFieldKey.title)
}
}
self.init(queryOutput: queryOutput)
}
Expand Down Expand Up @@ -386,6 +401,11 @@
get { self[#function] }
set { self[#function] = newValue }
}

package var _modificationDate: Date? {
get { modificationDate ?? self[#function] }
set { self[#function] = newValue }
}
}

extension DataProtocol {
Expand Down
7 changes: 7 additions & 0 deletions Sources/SQLiteData/CloudKit/Internal/MockCloudDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package let databaseScope: CKDatabase.Scope
let _container = IsolatedWeakVar<MockCloudContainer>()
let dataManager = Dependency(\.dataManager)
let now = Dependency(\.currentTime.now)
let uuid = Dependency(\.uuid)
let clock: AnyClock<Duration>

Expand Down Expand Up @@ -224,6 +225,9 @@

guard let databaseCopy = recordToSave.copy() as? CKRecord
else { fatalError("Could not copy CKRecord.") }
databaseCopy._modificationDate = Date(
timeIntervalSinceReferenceDate: Double(now.wrappedValue)
Comment thread
mbrandonw marked this conversation as resolved.
Resolved
)
databaseCopy._recordChangeTag = state.nextRecordChangeTag()

for key in databaseCopy.allKeys() {
Expand All @@ -249,6 +253,9 @@
as? CKRecord
{
parentRecord._recordChangeTag = state.nextRecordChangeTag()
parentRecord._modificationDate = Date(
timeIntervalSinceReferenceDate: Double(now.wrappedValue)
)
state.storage[parent.recordID.zoneID]?.records[parent.recordID] = parentRecord
}
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2121,8 +2121,8 @@
.execute(db)
}

if let lastKnownDate = metadata?.lastKnownServerRecord?.modificationDate {
if let recordDate = record.modificationDate, lastKnownDate < recordDate {
if let lastKnownDate = metadata?.lastKnownServerRecord?._modificationDate {
if let recordDate = record._modificationDate, lastKnownDate <= recordDate {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This <= was a legitimate bug. It essentially meant we never cached the record before sending it off to CloudKit.

try updateLastKnownServerRecord()
}
} else {
Expand Down
26 changes: 18 additions & 8 deletions Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -67,20 +67,22 @@
"""
}

let record = try syncEngine.private.database.record(for: Reminder.recordID(for: 1))
record.setValue("Buy milk", forKey: "title", at: 60)
let modificationCallback = try {
try syncEngine.modifyRecords(scope: .private, saving: [record])
}()
let modificationCallback = try withDependencies {
$0.currentTime.now += 60
} operation: {
let record = try syncEngine.private.database.record(for: Reminder.recordID(for: 1))
record.setValue("Buy milk", forKey: "title", at: now)
return try syncEngine.modifyRecords(scope: .private, saving: [record])
}

try await withDependencies {
$0.currentTime.now += 30
} operation: {
try await userDatabase.userWrite { db in
try Reminder.find(1).update { $0.isCompleted = true }.execute(db)
}
try await syncEngine.processPendingRecordZoneChanges(scope: .private)
}
try await syncEngine.processPendingRecordZoneChanges(scope: .private)

assertInlineSnapshot(of: container, as: .customDump) {
"""
Expand Down Expand Up @@ -126,8 +128,16 @@
"""
}

try await syncEngine.processPendingRecordZoneChanges(scope: .private)
await modificationCallback.notify()
try await withDependencies {
$0.currentTime.now += 30
} operation: {
try await syncEngine.processPendingRecordZoneChanges(scope: .private)
}
await withDependencies {
$0.currentTime.now += 60
} operation: {
await modificationCallback.notify()
}

assertInlineSnapshot(of: container, as: .customDump) {
"""
Expand Down
2 changes: 1 addition & 1 deletion Tests/SQLiteDataTests/Internal/CloudKit+CustomDump.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
}
var nonEncryptedKeys = Set(allKeys())
.subtracting(encryptedValues.allKeys())
.subtracting(["_recordChangeTag"])
.subtracting(["_modificationDate", "_recordChangeTag"])
if self is CKShare {
nonEncryptedKeys.insert(CKShare.SystemFieldKey.title)
}
Expand Down
Loading