I want to update my iOS project to support CloudKit sync with Core Data.
What exactly is the URL I have to define in storeDirectory
and what is the storeDirectory.appendingPathComponent
?
struct PersistenceController {
static var shared = PersistenceController()
var persistentContainer: NSPersistentCloudKitContainer
init() {
persistentContainer = NSPersistentCloudKitContainer(name: "iOwe")
let storeDirectory = URL(fileURLWithPath: "/")
try? FileManager.default.createDirectory(at: storeDirectory, withIntermediateDirectories: true, attributes: nil)
let storeURL = storeDirectory.appendingPathComponent("iOwe.xcdatamodeld")
let description = NSPersistentStoreDescription(url: storeURL)
description.configuration = "Cloud"
description.cloudKitContainerOptions = NSPersistentCloudKitContainerOptions(containerIdentifier: "de.haufs.iowe")
persistentContainer.persistentStoreDescriptions = [description]
persistentContainer.loadPersistentStores { storeDescription, error in
guard error == nil else {
fatalError("Could not load persistent stores. \(error!)")
}
}
}
}
The app crashes with
Thread 1: Fatal error: Could not load persistent stores. Error Domain=NSCocoaErrorDomain Code=513 "Die Datei konnte nicht gesichert werden, da du nicht über die erforderlichen Zugriffsrechte verfügst." UserInfo={reason=No permissions to create file; code = 1}