1

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}

HangarRash
  • 7,314
  • 5
  • 5
  • 32
hfs23
  • 125
  • 1
  • 1
  • 9
  • Please read the error message. It describes exactly what’s wrong. `"/"` represents the root folder of the operating system, which is not accessible by any user. You have to choose a folder in the container of the app. – vadian Nov 09 '22 at 13:12
  • I don't really understand which folder I have to choose. – hfs23 Nov 09 '22 at 13:22
  • Either the *Documents* or the *Application Support* folder. [Here](https://stackoverflow.com/questions/27721418/getting-list-of-files-in-documents-folder) are examples how to address the Documents folder. But use always the `FileManager` way. – vadian Nov 09 '22 at 15:35

0 Answers0