|
45 | 45 | } |
46 | 46 |
|
47 | 47 | modifications = state.withState { syncEngineState in |
48 | | - let currentChangeTag = syncEngineState.changeTag |
| 48 | + let currentChangeTag = syncEngineState.currentChangeTag |
49 | 49 | return database.state.withValue { dbState in |
50 | 50 | zoneIDs.reduce(into: [CKRecord]()) { accum, zoneID in |
51 | 51 | accum += ((dbState.storage[zoneID]?.records.values).map { Array($0) } ?? []) |
52 | 52 | .map { $0.copy() as! CKRecord } |
53 | 53 | .filter { |
54 | | - precondition( |
55 | | - $0._recordChangeTag != nil, |
56 | | - "Records stored in database should have their 'recordChangeTag' assigned." |
57 | | - ) |
58 | | - return $0._recordChangeTag! > currentChangeTag |
| 54 | + guard let changeTag = $0._recordChangeTag else { |
| 55 | + fatalError( |
| 56 | + "Records stored in database should have their 'recordChangeTag' assigned." |
| 57 | + ) |
| 58 | + } |
| 59 | + return changeTag.isNewerChangeTag(than: currentChangeTag) |
59 | 60 | } |
60 | 61 | } |
61 | 62 | } |
|
75 | 76 | else { return } |
76 | 77 |
|
77 | 78 | state.withState { |
78 | | - $0.changeTag = modifications.compactMap(\._recordChangeTag).max() ?? $0.changeTag |
| 79 | + for changeTag in modifications.compactMap(\._recordChangeTag) { |
| 80 | + if changeTag.isNewerChangeTag(than: $0.currentChangeTag) { |
| 81 | + $0.currentChangeTag = changeTag |
| 82 | + } |
| 83 | + } |
79 | 84 | } |
80 | 85 |
|
81 | 86 | await parentSyncEngine.handleEvent( |
|
134 | 139 | @available(iOS 17, macOS 14, tvOS 17, watchOS 10, *) |
135 | 140 | package final class MockSyncEngineState: CKSyncEngineStateProtocol { |
136 | 141 | package struct State: Sendable { |
137 | | - package var changeTag = 0 |
| 142 | + package var currentChangeTag: String? |
138 | 143 | package var hasPendingUntrackedChanges = false |
139 | 144 | package var pendingDatabaseChanges: |
140 | 145 | OrderedDictionary<CKRecordZone.ID, CKSyncEngine.PendingDatabaseChange> = [:] |
|
513 | 518 | } |
514 | 519 | } |
515 | 520 | } |
| 521 | + |
| 522 | + extension String { |
| 523 | + package func isNewerChangeTag(than other: String?) -> Bool { |
| 524 | + guard let other else { return true } |
| 525 | + return self.count != other.count ? self.count > other.count : self > other |
| 526 | + } |
| 527 | + } |
516 | 528 | #endif |
0 commit comments