|
819 | 819 | deletions: [(recordID: CKRecord.ID, recordType: CKRecord.RecordType)] = [], |
820 | 820 | syncEngine: any SyncEngineProtocol |
821 | 821 | ) async { |
| 822 | + let recordIDsByRecordType = OrderedDictionary( |
| 823 | + grouping: deletions.sorted { lhs, rhs in |
| 824 | + guard |
| 825 | + let lhsIndex = tablesByOrder[lhs.recordType], |
| 826 | + let rhsIndex = tablesByOrder[rhs.recordType] |
| 827 | + else { return true } |
| 828 | + return lhsIndex > rhsIndex |
| 829 | + }, |
| 830 | + by: \.recordType |
| 831 | + ) |
| 832 | + .mapValues { $0.map(\.recordID) } |
| 833 | + for (recordType, recordIDs) in recordIDsByRecordType { |
| 834 | + let recordPrimaryKeys = recordIDs.compactMap(\.recordPrimaryKey) |
| 835 | + if let table = tablesByName[recordType] { |
| 836 | + func open<T: PrimaryKeyedTable>(_: T.Type) { |
| 837 | + withErrorReporting(.sqliteDataCloudKitFailure) { |
| 838 | + try userDatabase.write { db in |
| 839 | + try T |
| 840 | + .where { |
| 841 | + $0.primaryKey.in( |
| 842 | + recordPrimaryKeys.map { SQLQueryExpression("\(bind: $0)") } |
| 843 | + ) |
| 844 | + } |
| 845 | + .delete() |
| 846 | + .execute(db) |
| 847 | + |
| 848 | + try UnsyncedRecordID |
| 849 | + .findAll(recordIDs) |
| 850 | + .delete() |
| 851 | + .execute(db) |
| 852 | + } |
| 853 | + } |
| 854 | + } |
| 855 | + open(table) |
| 856 | + } else if recordType == CKRecord.SystemType.share { |
| 857 | + withErrorReporting { |
| 858 | + for recordID in recordIDs { |
| 859 | + try deleteShare(recordID: recordID) |
| 860 | + } |
| 861 | + } |
| 862 | + } else { |
| 863 | + // NB: Deleting a record from a table we do not currently recognize. |
| 864 | + } |
| 865 | + } |
| 866 | + |
822 | 867 | let unsyncedRecords = |
823 | 868 | await withErrorReporting(.sqliteDataCloudKitFailure) { |
824 | 869 | var unsyncedRecordIDs = try await userDatabase.write { db in |
|
833 | 878 | unsyncedRecordIDs.subtract(modificationRecordIDs) |
834 | 879 | if !unsyncedRecordIDsToDelete.isEmpty { |
835 | 880 | try await userDatabase.write { db in |
836 | | - for recordID in unsyncedRecordIDsToDelete { |
837 | | - try UnsyncedRecordID.find(recordID).delete().execute(db) |
838 | | - } |
| 881 | + try UnsyncedRecordID |
| 882 | + .findAll(unsyncedRecordIDsToDelete) |
| 883 | + .delete() |
| 884 | + .execute(db) |
839 | 885 | } |
840 | 886 | } |
841 | 887 | let results = try await syncEngine.database.records(for: Array(unsyncedRecordIDs)) |
|
902 | 948 | } |
903 | 949 | } |
904 | 950 | } |
905 | | - |
906 | | - let recordIDsByRecordType = OrderedDictionary( |
907 | | - grouping: deletions.sorted { lhs, rhs in |
908 | | - guard |
909 | | - let lhsIndex = tablesByOrder[lhs.recordType], |
910 | | - let rhsIndex = tablesByOrder[rhs.recordType] |
911 | | - else { return true } |
912 | | - return lhsIndex > rhsIndex |
913 | | - }, |
914 | | - by: \.recordType |
915 | | - ) |
916 | | - .mapValues { $0.map(\.recordID) } |
917 | | - for (recordType, recordIDs) in recordIDsByRecordType { |
918 | | - let recordPrimaryKeys = recordIDs.compactMap(\.recordPrimaryKey) |
919 | | - if let table = tablesByName[recordType] { |
920 | | - func open<T: PrimaryKeyedTable>(_: T.Type) { |
921 | | - withErrorReporting(.sqliteDataCloudKitFailure) { |
922 | | - try userDatabase.write { db in |
923 | | - try T |
924 | | - .where { |
925 | | - $0.primaryKey.in( |
926 | | - recordPrimaryKeys.map { SQLQueryExpression("\(bind: $0)") } |
927 | | - ) |
928 | | - } |
929 | | - .delete() |
930 | | - .execute(db) |
931 | | - } |
932 | | - } |
933 | | - } |
934 | | - open(table) |
935 | | - } else if recordType == CKRecord.SystemType.share { |
936 | | - withErrorReporting { |
937 | | - for recordID in recordIDs { |
938 | | - try deleteShare(recordID: recordID) |
939 | | - } |
940 | | - } |
941 | | - } else { |
942 | | - // NB: Deleting a record from a table we do not currently recognize. |
943 | | - } |
944 | | - } |
945 | 951 | } |
946 | 952 |
|
947 | 953 | package func handleSentRecordZoneChanges( |
|
0 commit comments