Thank you, this is very helpful. To confirm we're aligned:
Open the database in read-only mode
If loadPersistentStores fails, try it again in read-write mode
If that succeeds destroy those CoreData object and do a read-only open again
If that fails show an error message to the user
destroy those CoreData object
Is that accomplished via container.persistentStoreCoordinator.destroyPersistentStore, so all together like so?
let container = NSPersistentCloudKitContainer(name: "AppName")
let sharedStoreURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.domain.appname")!.appendingPathComponent("\(container.name).sqlite")
guard FileManager.default.fileExists(atPath: sharedStoreURL.path) else {
// Show error to user
return
}
let description = container.persistentStoreDescriptions.first!
description.url = sharedStoreURL
description.isReadOnly = true
container.loadPersistentStores { description, error in
if let error {
container.persistentStoreDescriptions.first?.isReadOnly = false
container.loadPersistentStores { description, error in
if let error {
// Show error to user
} else {
try? container.persistentStoreCoordinator.destroyPersistentStore(at: sharedStoreURL, type: .sqlite)
container.persistentStoreDescriptions.first?.isReadOnly = true
container.loadPersistentStores { description, error in
if let error {
// Show error to user
}
}
}
}
}
}
Topic:
App & System Services
SubTopic:
iCloud & Data
Tags: