Resume CloudKit sync from persisted state instead of full re-fetch

Previously CKSyncEngine was created with nil state serialization on
every launch, replaying the entire zone change history (hundreds of
merge/delete log lines and re-downloads each start). Now the saved
state is loaded so launches only fetch changes since the last session.

To make incremental sync safe:
- Clear persisted state once when the record schema version increases,
  so records previously skipped as unsupported get re-delivered after
  an app update.
- Handle remote zone deletion in fetchedDatabaseChanges by recreating
  the zone and re-uploading local data, instead of leaving sync dead.

Account change and manual Refresh Sync still clear the state and force
a full fetch as before.
This commit is contained in:
Arkadiusz Fal
2026-06-23 23:54:40 +02:00
parent 7808d4c730
commit da6c949f51
2 changed files with 63 additions and 10 deletions

View File

@@ -22,7 +22,8 @@ final class CloudKitRecordMapper {
/// Current schema version for CloudKit records.
/// Increment this when record structure changes.
private let currentSchemaVersion: Int64 = 2
static let currentSchemaVersion: Int64 = 2
private var currentSchemaVersion: Int64 { Self.currentSchemaVersion }
init(zone: CKRecordZone) {
self.zone = zone