Hi,
I’m trying to migrate Core Data with CloudKit to the shared App Group Container so that widgets can get access to it.
So far, I’ve managed to do so with the following code:
init() {
container = NSPersistentCloudKitContainer(name: “AppName”)
let storeDescription = container.persistentStoreDescriptions.first!
// Clean up code goes here (explained later)
// ...
let sharedGroupStoreURL = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: "group.com.***")!.appendingPathComponent("AppName.sqlite")
storeDescription.url = sharedGroupStoreURL
container.loadPersistentStores { (storeDescription, error) in
if let error = error as NSError? {
print("Unresolved error \(error), \(error.userInfo)")
}
}
container.viewContext.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
container.viewContext.automaticallyMergesChangesFromParent = true
}
This tells Core Data to use the shared container. When the new persistentStore loads, it gets seamlessly populated with the data from CloudKit.
So far, so good. My main issue is, how do I clean up the data remaining in my app from the old NSPersistentStore?
I’ve done it adding the following clean up code:
if let defaultURL = storeDescription.url {
if FileManager.default.fileExists(atPath: defaultURL.path) {
let fileCoordinator = NSFileCoordinator(filePresenter: nil)
fileCoordinator.coordinate(writingItemAt: defaultURL.deletingLastPathComponent(), options: .forDeleting, error: nil) { url in
do {
try FileManager.default.removeItem(at: url)
} catch {
print(error.localizedDescription)
}
}
}
}
This deletes the following files:
ckAssetFiles
AppName.sqlite
.AppName_SUPPORT
AppName.sqlite-wal
AppName.sqlite-shm
Is this the right way to do it? Are there any other files that should be deleted to?
Thanks,
Àlex García.
Post
Replies
Boosts
Views
Activity
Using SwiftUI on macOS, is there a way to make a NavigationView without a Sidebar?
With the following code:
NavigationView {
EmptyView()
.frame(minWidth: 0, idealWidth: 0, maxWidth: 0)
ScrollView {
HStack {
Text("Text 1")
Spacer()
}
}
.toolbar {
Button("Button 1") {}
}
ScrollView {
Text("Text 2")
}
.toolbar {
Button("Button 2") {}
}
}
.navigationTitle("Title")
I get this result:
But what I want to achieve is this:
Kind of like this window from Shortcuts (which is said to be built with SwiftUI):