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: 10 additions & 10 deletions Package.resolved

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 11 additions & 15 deletions Sources/SQLiteData/CloudKit/SyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Comment on lines -1094 to -1097

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.

For tables that have no references, these values would be nil, and we would fallback to returning true below, which ultimately can cause any value to be shifted around while sorting. We already have a helper for properly comparing tablesByOrder values, which is topologicallyAscending, and it further sorts tables with no references alphabetically after all tables with references.

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
}
}

Expand Down
26 changes: 26 additions & 0 deletions Tests/SQLiteDataTests/CloudKitTests/CloudKitTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Loading