From b36e2114a42f5cea81f9930004bcacce0d07b397 Mon Sep 17 00:00:00 2001 From: Mojtaba Hosseini Date: Sun, 16 Aug 2026 19:04:46 +0330 Subject: [PATCH] feat: add a method to force sync the syncEngine without deleting the local data --- Sources/SQLiteData/CloudKit/SyncEngine.swift | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/Sources/SQLiteData/CloudKit/SyncEngine.swift b/Sources/SQLiteData/CloudKit/SyncEngine.swift index 036ff3fa..90d0a8a2 100644 --- a/Sources/SQLiteData/CloudKit/SyncEngine.swift +++ b/Sources/SQLiteData/CloudKit/SyncEngine.swift @@ -759,17 +759,26 @@ /// > Important: It is only appropriate to call this method when the device's iCloud account /// > logs out or changes. public func deleteLocalData() async throws { + try await refreshLocalData(shouldReset: true) + } + + /// Restarts the sync engine and triggers a new synchronization cycle. + /// This works around a known `CKSyncEngine` behavior where pending + /// remote changes may not be fetched until another sync trigger occurs. + public func refreshLocalData(shouldReset: Bool = false) async throws { stop() try tearDownSyncEngine() await withErrorReporting(.sqliteDataCloudKitFailure) { try await userDatabase.write { db in - for table in tables { - func open(_: some SynchronizableTable) { - withErrorReporting(.sqliteDataCloudKitFailure) { - try T.delete().execute(db) + if shouldReset { + for table in tables { + func open(_: some SynchronizableTable) { + withErrorReporting(.sqliteDataCloudKitFailure) { + try T.delete().execute(db) + } } + open(table) } - open(table) } try setUpSyncEngine(writableDB: db) }