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
56 changes: 41 additions & 15 deletions Sources/SQLiteData/CloudKit/Internal/MockSyncEngine.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@
package struct State: Sendable {
package var changeTag = 0
package var hasPendingUntrackedChanges = false
package var pendingDatabaseChanges: OrderedSet<CKSyncEngine.PendingDatabaseChange> = []
package var pendingRecordZoneChanges: OrderedSet<CKSyncEngine.PendingRecordZoneChange> = []
package var pendingDatabaseChanges:
OrderedDictionary<CKRecordZone.ID, CKSyncEngine.PendingDatabaseChange> = [:]
package var pendingRecordZoneChanges:
OrderedDictionary<CKRecord.ID, CKSyncEngine.PendingRecordZoneChange> = [:]
}

private let state = LockIsolated(State())
Expand Down Expand Up @@ -165,11 +167,11 @@
}

package var pendingRecordZoneChanges: [CKSyncEngine.PendingRecordZoneChange] {
withState { Array($0.pendingRecordZoneChanges) }
withState { Array($0.pendingRecordZoneChanges.values) }
}

package var pendingDatabaseChanges: [CKSyncEngine.PendingDatabaseChange] {
withState { Array($0.pendingDatabaseChanges) }
withState { Array($0.pendingDatabaseChanges.values) }
}

package var hasPendingUntrackedChanges: Bool {
Expand All @@ -186,26 +188,46 @@
}

package func add(pendingRecordZoneChanges: [CKSyncEngine.PendingRecordZoneChange]) {
withState {
$0.pendingRecordZoneChanges.append(contentsOf: pendingRecordZoneChanges)
withState { state in
for change in pendingRecordZoneChanges {
guard let id = change.id
else { fatalError("Unsupported pendingRecordZoneChange: \(change)") }
state.pendingRecordZoneChanges[id] = change
}
}
}

package func remove(pendingRecordZoneChanges: [CKSyncEngine.PendingRecordZoneChange]) {
withState {
$0.pendingRecordZoneChanges.subtract(pendingRecordZoneChanges)
withState { state in
for change in pendingRecordZoneChanges {
guard let id = change.id
else { fatalError("Unsupported pendingRecordZoneChange: \(change)") }
if state.pendingRecordZoneChanges[id] == change {
state.pendingRecordZoneChanges.removeValue(forKey: id)
}
}
}
}

package func add(pendingDatabaseChanges: [CKSyncEngine.PendingDatabaseChange]) {
withState {
$0.pendingDatabaseChanges.append(contentsOf: pendingDatabaseChanges)
withState { state in
for change in pendingDatabaseChanges {
guard let zoneID = change.zoneID
else { fatalError("Unsupported pendingDatabaseChange: \(change)") }
state.pendingDatabaseChanges[zoneID] = change
}
}
}

package func remove(pendingDatabaseChanges: [CKSyncEngine.PendingDatabaseChange]) {
withState {
$0.pendingDatabaseChanges.subtract(pendingDatabaseChanges)
withState { state in
for change in pendingDatabaseChanges {
guard let zoneID = change.zoneID
else { fatalError("Unsupported pendingDatabaseChange: \(change)") }
if state.pendingDatabaseChanges[zoneID] == change {
state.pendingDatabaseChanges.removeValue(forKey: zoneID)
}
}
}
}
}
Expand Down Expand Up @@ -409,7 +431,7 @@
return changes
}

for pendingDatabaseChange in syncEngineState.pendingDatabaseChanges {
for (_, pendingDatabaseChange) in syncEngineState.pendingDatabaseChanges {
switch pendingDatabaseChange {
case .saveZone(let zone):
changes.zonesToSave.append(zone)
Expand All @@ -434,7 +456,9 @@
case .success(let zone):
changes.savedZones.append(zone)
case .failure(let error as CKError):
changes.failedZoneSaves.append((changes.zonesToSave.first(where: { $0.zoneID == zoneID })!, error))
changes.failedZoneSaves.append(
(changes.zonesToSave.first(where: { $0.zoneID == zoneID })!, error)
)
case .failure(let error):
reportIssue("Error thrown not CKError: \(error)")
}
Expand All @@ -454,7 +478,9 @@
}

syncEngine.state.remove(pendingDatabaseChanges: changes.savedZones.map { .saveZone($0) })
syncEngine.state.remove(pendingDatabaseChanges: changes.deletedZoneIDs.map { .deleteZone($0) })
syncEngine.state.remove(
pendingDatabaseChanges: changes.deletedZoneIDs.map { .deleteZone($0) }
)
await syncEngine.parentSyncEngine
.handleEvent(
.sentDatabaseChanges(
Expand Down
15 changes: 10 additions & 5 deletions Sources/SQLiteData/CloudKit/Internal/Triggers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,15 @@
parentRecordPrimaryKey,
parentRecordType
)
} onConflictDoUpdate: { _ in
} onConflictDoUpdate: {
$0._isDeleted = false
$0.userModificationTime = $currentTime()
$0.zoneName = zoneName ?? $0.zoneName
$0.ownerName = ownerName ?? $0.ownerName
$0.parentRecordPrimaryKey = parentRecordPrimaryKey
$0.parentRecordType = parentRecordType
} where: {
$0._isDeleted
}
}

Expand Down Expand Up @@ -234,10 +242,7 @@
$0.userModificationTime = $currentTime()
}
}
}

@available(iOS 17, macOS 14, tvOS 17, watchOS 10, *)
extension SyncMetadata {
static func callbackTriggers(for syncEngine: SyncEngine) -> [TemporaryTrigger<Self>] {
[
afterInsertTrigger(for: syncEngine),
Expand Down Expand Up @@ -325,7 +330,7 @@
)
)
} when: { old, new in
old._isDeleted.eq(new._isDeleted) && !SyncEngine.$isSynchronizing
!new._isDeleted && !SyncEngine.$isSynchronizing
}
)
}
Expand Down
8 changes: 8 additions & 0 deletions Sources/SQLiteData/CloudKit/Internal/UserDatabase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@
database.configuration
}

package func unsafeReentrantWrite<T>(_ updates: (Database) throws -> T) rethrows -> T {
try database.unsafeReentrantWrite { db in
try $_isSynchronizingChanges.withValue(true) {
try updates(db)
}
}
}

package func write<T: Sendable>(
_ updates: @Sendable (Database) throws -> T
) async throws -> T {
Expand Down
Loading
Loading