I think CoreData+iCloud hates me.

Hey All,

I'm starting the journey to incorporating Core-Data-iCloud syncing into my App for the first itme. I'm following the guide located here:

https://developer.apple.com/library/ios/documentation/DataManagement/Conceptual/UsingCoreDataWithiCloudPG/UsingSQLiteStoragewithiCloud/UsingSQLiteStoragewithiCloud.html


Sure enough, on the first "checkpoint" I'm already having issues. My app reports:

-[PFUbiquitySetupAssistant finishSetupWithRetry:](830): CoreData: Ubiquity: <PFUbiquitySetupAssistant: 0x1257a62f0>: Retrying after delay: 1

Error Domain=NSCocoaErrorDomain Code=134080 "An error occurred while opening the persistent store." UserInfo={Reason=Didn't get a container URL back from URLForUbiquityContainerIdentifier:, giving up now. Please ensure the application is signed with the proper entitlements to read from the container., NSPersistentStoreUbiquitousContainerIdentifierKey=null}


I checked the entitlements and they look okay... They appear correctly setup by Xcode.

I have:

com.apple.developer.icloud-container-identifiers = iCloud.$(CFBundleIdentifier)

com.apple.developer.icloud-services = CloudKit

iCloud Key-Value Store = $(TeamIdentifierPrefix)$(CFBundleIdentifier) [This one is for after I get the core-data stuff workin]

I've verified that my AppID has "iCloud" activated on it.


I don't really know what else to check... The guide doesnt elaborate on any troubleshooting at this point. You guys have any clues?


Oh yea, PSC code:

  NSError *error = nil;
  _defaultPersistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self defaultManagedObjectModel]];

    NSDictionary* storeOptions = @{NSMigratePersistentStoresAutomaticallyOption: [NSNumber numberWithBool:YES],
                                   NSInferMappingModelAutomaticallyOption: [NSNumber numberWithBool:YES],
                                   NSPersistentStoreUbiquitousContentNameKey: @"CENSOREDCloudStore"};
   
  if (![ _defaultPersistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType
                                                         configuration:nil
                                                                   URL:self.storeURL
                                                               options:storeOptions
                                                                 error:&error]) {

Replies

You're missing the CloudDocuments service (which is required by CoreData's iCloud integration). You should see both CloudKit (if you desire it) and CloudDocuments in your entitlements


<key>com.apple.developer.icloud-services</key>
<array>
<string>CloudDocuments</string>
<string>CloudKit</string>
</array>


It's worth noting that this API (CoreData + iCloud Drive) is deprecated in iOS 10 / macOS 10.12. For a new application I would recommend using CloudKit instead.

We are switching right now from a Core Data + iCloud setup to a Core Data + CloudKit setup. Unfortunately, we can't find any sort of good sample from Apple to guide us through. Can you please help us with some links or references?


Our users are suffering from random data loss and this is a plague that is happening because of iCloud. We want to fix that, but we don't want to change to Realm. So please guys, share with us some best practices how Apple handles syncing and local persistence in their new apps.


Cheers,

P

Have you looked at previous WWDC videos about CloudKit? Implementing a CoreData <-> CloudKit-Sync is lots, LOTS of hard work but can be done (of course, depending on the data setup in your app)