From d454c49a7b02450db33adc32fb81c24970a7445e Mon Sep 17 00:00:00 2001 From: Lukas Kubanek Date: Wed, 11 Nov 2026 16:02:56 +0100 Subject: [PATCH 1/2] Correct expectation for conflict-on-fetch scenario Refs #354 --- .../SQLiteDataTests/CloudKitTests/MergeConflictTests.swift | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift b/Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift index a37a8987..d5ef7fe1 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/MergeConflictTests.swift @@ -416,7 +416,8 @@ } @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) - @Test func serverRecordEditedAfterClientButProcessedBeforeClient() async throws { + @Test(.bug("https://github.com/pointfreeco/sqlite-data/issues/354")) + func serverRecordEditedAfterClientButProcessedBeforeClient() async throws { try await userDatabase.userWrite { db in try db.seed { RemindersList(id: 1, title: "") @@ -453,7 +454,7 @@ │ dueDate: nil, │ │ isCompleted: false, │ │ priority: nil, │ - │ title: "Get milk", │ + │ title: "Buy milk", │ │ remindersListID: 1 │ │ ) │ └───────────────────────┘ @@ -478,7 +479,7 @@ priority🗓️: 0, remindersListID: 1, remindersListID🗓️: 0, - title: "Get milk", + title: "Buy milk", title🗓️: 60, 🗓️: 60 ), From 5e14cd61a0939df5871d29a768ddab15283befb6 Mon Sep 17 00:00:00 2001 From: Lukas Kubanek Date: Wed, 11 Nov 2026 16:50:23 +0100 Subject: [PATCH 2/2] Fix conflict-on-fetch scenario Fixes #354 Fixes #274 --- .../CloudKit/CloudKit+StructuredQueries.swift | 11 +++++++++-- Sources/SQLiteData/CloudKit/SyncEngine.swift | 1 + 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift b/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift index a6b216da..7a098575 100644 --- a/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift +++ b/Sources/SQLiteData/CloudKit/CloudKit+StructuredQueries.swift @@ -308,6 +308,7 @@ func update( with other: CKRecord, row: T, + userModificationTime: Int64, columnNames: inout [String], parentForeignKey: ForeignKey? ) { @@ -354,11 +355,17 @@ return false } } - if didSet || isRowValueModified { + if didSet { columnNames.removeAll(where: { $0 == key }) - if didSet, let parentForeignKey, key == parentForeignKey.from { + if let parentForeignKey, key == parentForeignKey.from { self.parent = other.parent } + } else if isRowValueModified { + let hasServerValueChanged = encryptedValues[at: key] > other.encryptedValues[at: key] + let isRowValueNewer = userModificationTime >= encryptedValues[at: key] + if !hasServerValueChanged || isRowValueNewer { + columnNames.removeAll(where: { $0 == key }) + } } } open(column) diff --git a/Sources/SQLiteData/CloudKit/SyncEngine.swift b/Sources/SQLiteData/CloudKit/SyncEngine.swift index 95cded1f..731a8484 100644 --- a/Sources/SQLiteData/CloudKit/SyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/SyncEngine.swift @@ -2122,6 +2122,7 @@ serverRecord.update( with: allFields, row: T(queryOutput: row), + userModificationTime: metadata.userModificationTime, columnNames: &columnNames, parentForeignKey: foreignKeysByTableName[T.tableName]?.count == 1 ? foreignKeysByTableName[T.tableName]?.first