Re-fetch everything from iCloud when the local store was recreated

On tvOS SwiftData keeps the store in the app group's Library/Caches,
which the system may purge. The CKSyncEngine tokens live in UserDefaults
and survive, so the next launch opened an empty store and fetched only
records changed since the old tokens; subscriptions and bookmarks that
had not changed since never came back.

DataManager now records whether the store file existed when the
container opened, and the sync engine clears its persisted state once
per launch when it did not, so the first sync after a loss is a full
fetch. Also log the store path and a persistent-store failure that
previously fell back to in-memory storage silently.
This commit is contained in:
Arkadiusz Fal
2026-10-07 23:59:56 +02:00
parent bc6ad55f38
commit b9e7d88a12
3 changed files with 38 additions and 1 deletions

View File

@@ -25,6 +25,16 @@ final class DataManager {
/// Whether CloudKit sync is currently enabled for this instance.
private(set) var isCloudKitEnabled: Bool = false
/// Whether the on-disk store did not exist when this container was opened.
///
/// On tvOS SwiftData keeps the store in Library/Caches, which the system
/// purges under storage pressure. The CloudKit sync tokens live in
/// UserDefaults and survive such a purge, so a fresh store must be reported
/// to the sync engine — otherwise it only fetches changes made since the
/// old tokens and records that did not change since (subscriptions,
/// bookmarks) never come back.
private(set) var storeWasCreated: Bool = false
/// Cached set of bookmarked video IDs for fast O(1) lookup.
/// Updated when bookmarks are added/removed locally or via CloudKit sync.
@@ -71,15 +81,20 @@ final class DataManager {
)
}
let storeExisted = inMemory || FileManager.default.fileExists(atPath: configuration.url.path)
self.modelContainer = try ModelContainer(
for: Self.schema,
configurations: [configuration]
)
self.storeWasCreated = !storeExisted
self.modelContext = modelContainer.mainContext
self.modelContext.autosaveEnabled = true
self.isCloudKitEnabled = false
LoggingService.shared.logCloudKit("SwiftData initialized with local storage (CloudKit sync via CKSyncEngine)")
LoggingService.shared.logCloudKit(
"SwiftData initialized with local storage (CloudKit sync via CKSyncEngine)",
details: inMemory ? "in-memory" : "store: \(configuration.url.path), existed: \(storeExisted)"
)
// Initialize bookmark cache
refreshBookmarkCache()