From 02a8372385fb7db42a787804c9157ab79cef7933 Mon Sep 17 00:00:00 2001 From: Brandon Williams Date: Mon, 2 Nov 2026 20:16:37 -0600 Subject: [PATCH] Fix sorting of records when processing batches. --- Package.resolved | 20 +++++++------- Sources/SQLiteData/CloudKit/SyncEngine.swift | 26 ++++++++----------- .../CloudKitTests/CloudKitTests.swift | 26 +++++++++++++++++++ 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/Package.resolved b/Package.resolved index 1e6800d8..02fc42b9 100644 --- a/Package.resolved +++ b/Package.resolved @@ -1,5 +1,5 @@ { - "originHash" : "dfd7e0f6bddf53d83bc4372f08b665b419a79785b6e33925d051440a0a13ec00", + "originHash" : "f7503587ba7ade48448be5b58ac6834c7ebba67a5e6f166428a40a87b9921413", "pins" : [ { "identity" : "combine-schedulers", @@ -91,6 +91,15 @@ "version" : "1.1.1" } }, + { + "identity" : "swift-issue-reporting", + "kind" : "remoteSourceControl", + "location" : "https://github.com/pointfreeco/swift-issue-reporting", + "state" : { + "revision" : "71c7c9a761d1ca6ed4ccb6ced040fe1c1a39e8e7", + "version" : "2.1.0" + } + }, { "identity" : "swift-perception", "kind" : "remoteSourceControl", @@ -135,15 +144,6 @@ "revision" : "79e4b74a295b6eb74a8b585e3a39d29e70c1dbd1", "version" : "603.0.2" } - }, - { - "identity" : "xctest-dynamic-overlay", - "kind" : "remoteSourceControl", - "location" : "https://github.com/pointfreeco/xctest-dynamic-overlay", - "state" : { - "revision" : "d9308ad679143c26a30dec52daefe82f54af2b82", - "version" : "1.13.1" - } } ], "version" : 3 diff --git a/Sources/SQLiteData/CloudKit/SyncEngine.swift b/Sources/SQLiteData/CloudKit/SyncEngine.swift index 036ff3fa..7497a463 100644 --- a/Sources/SQLiteData/CloudKit/SyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/SyncEngine.swift @@ -1090,27 +1090,23 @@ changes.sort { lhs, rhs in switch (lhs, rhs) { case (.saveRecord(let lhs), .saveRecord(let rhs)): - guard - let lhsRecordType = lhs.tableName, - let lhsIndex = tablesByOrder[lhsRecordType], - let rhsRecordType = rhs.tableName, - let rhsIndex = tablesByOrder[rhsRecordType] - else { return true } - return lhsIndex < rhsIndex + return topologicallyAscending( + lhsTableName: lhs.tableName, + rhsTableName: rhs.tableName, + rootFirst: true + ) case (.deleteRecord(let lhs), .deleteRecord(let rhs)): - guard - let lhsRecordType = lhs.tableName, - let lhsIndex = tablesByOrder[lhsRecordType], - let rhsRecordType = rhs.tableName, - let rhsIndex = tablesByOrder[rhsRecordType] - else { return true } - return lhsIndex > rhsIndex + return topologicallyAscending( + lhsTableName: lhs.tableName, + rhsTableName: rhs.tableName, + rootFirst: false + ) case (.saveRecord, .deleteRecord): return false case (.deleteRecord, .saveRecord): return true default: - return true + return false } } diff --git a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift index fa989369..219af38b 100644 --- a/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift +++ b/Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift @@ -927,5 +927,31 @@ } } } + + @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) + @Test func sendParentsBeforeChildren() async throws { + try await userDatabase.userWrite { db in + for id in 1...30 { + try RemindersList.insert { RemindersList(id: id, title: "List \(id)") }.execute(db) + } + for id in 1...30 { + try ScopedModel.insert { ScopedModel(id: id, title: "Model \(id)") }.execute(db) + } + for id in 1...30 { + try Reminder.insert { + Reminder(id: id, title: "Reminder \(id)", remindersListID: id) + } + .execute(db) + } + } + + let batch = try #require( + await syncEngine.nextRecordZoneChangeBatch(syncEngine: syncEngine.private) + ) + let recordTypes = batch.recordsToSave.map(\.recordType) + let lastListIndex = try #require(recordTypes.lastIndex(of: RemindersList.tableName)) + let firstReminderIndex = try #require(recordTypes.firstIndex(of: Reminder.tableName)) + #expect(lastListIndex < firstReminderIndex) + } } #endif