iOS Core Data + iCloud error for addPersistentStor

2019-04-02 17:27发布

问题:

I've created a Core Data project from scratch, the only change I made is that where the persistent coordinator is adding a store, I'm providing a NSPersistentStoreUbiquitousContentNameKey option:

coordinator.addPersistentStoreWithType(NSSQLiteStoreType, configuration: nil, URL: url, options: [NSPersistentStoreUbiquitousContentNameKey : "coreDataiCloudTestStore"])

and I'm inserting and saving a new managed object at launch.

When at launch there is no iCloud account logged in, no problems. While the app is running, I'm logging into iCloud and going back to the app. I'm getting this error: You don’t have permission to save the file “store” in the folder, operation not permitted:

CoreData: error: -addPersistentStoreWithType:SQLite configuration:PF_DEFAULT_CONFIGURATION_NAME URL:file:///var/mobile/Containers/Data/Application/9032B2BC-BE8B-42C9-A9CC-EF3E01AA6F2B/Documents/SingleViewCoreData.sqlite options:{ NSPersistentStoreUbiquitousContentNameKey = coreDataiCloudTestStore; PFUbiquitySetupSynchronousSideLoadKey = 1; }

... returned error Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “store” in the folder “380E290C-97F9-4DC3-9B3C-43322FCA455D”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/9032B2BC-BE8B-42C9-A9CC-EF3E01AA6F2B/Documents/CoreDataUbiquitySupport/mobile~C5A2ECE3-1AB9-45FA-9C15-037DCCA054DE/coreDataiCloudTestStore/380E290C-97F9-4DC3-9B3C-43322FCA455D/store, NSUnderlyingError=0x1453f430 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}} with userInfo dictionary { NSFilePath = "/var/mobile/Containers/Data/Application/9032B2BC-BE8B-42C9-A9CC-EF3E01AA6F2B/Documents/CoreDataUbiquitySupport/mobile~C5A2ECE3-1AB9-45FA-9C15-037DCCA054DE/coreDataiCloudTestStore/380E290C-97F9-4DC3-9B3C-43322FCA455D/store"; NSUnderlyingError = "Error Domain=NSPOSIXErrorDomain Code=1 \"Operation not permitted\""; }

PFUbiquitySwitchboardEntry containerIdentifierChanged:: CoreData: Ubiquity: Error loading new temporary coordinator after account change with options { { NSPersistentStoreUbiquitousContentNameKey = coreDataiCloudTestStore; } } and URL file:///var/mobile/Containers/Data/Application/9032B2BC-BE8B-42C9-A9CC-EF3E01AA6F2B/Documents/SingleViewCoreData.sqlite and error = Error Domain=NSCocoaErrorDomain Code=513 "You don’t have permission to save the file “store” in the folder “380E290C-97F9-4DC3-9B3C-43322FCA455D”." UserInfo={NSFilePath=/var/mobile/Containers/Data/Application/9032B2BC-BE8B-42C9-A9CC-EF3E01AA6F2B/Documents/CoreDataUbiquitySupport/mobile~C5A2ECE3-1AB9-45FA-9C15-037DCCA054DE/coreDataiCloudTestStore/380E290C-97F9-4DC3-9B3C-43322FCA455D/store, NSUnderlyingError=0x1453f430 {Error Domain=NSPOSIXErrorDomain Code=1 "Operation not permitted"}}

Why is this happening?

回答1:

  • Probably this is due to the options that you pass in when adding persistent store,
  • It happens when you don't let the core data handles the rebuilding icloud-enabled persistent store,
  • You must enable the NSPersistentStoreRebuildFromUbiquitousContentOption flag in the options

    Look at the apple doc https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html

Rebuilding from iCloud

To remove local data and start fresh with the existing records in iCloud, pass the NSPersistentStoreRebuildFromUbiquitousContentOption option with a value of @YES when you add your iCloud-enabled persistent store to the persistent store coordinator. Include any iCloud options you usually include.