3

I'm trying to add iCloud functionality to an existing app. I'm using UIManagedDocument to reference Core Data. I've put a fair amount of research into this and I can't figure out what I'm doing wrong.

I've tried getting this to work on my Macbook and on my Mac Mini. I've tried turning off the iCloud capability, exiting Xcode, then reopening Xcode and turning iCloud capability back on. No luck.

The reason why I don't think it's entitled is because:

  1. I'm not seeing the Cloud Debug Gauge.
  2. When I trigger an iCloud sync in the simulator I get an error stating:

The operation couldn’t be completed. (BRCloudDocsErrorDomain error 2 - Logged out - iCloud Drive is not configured)

  1. The following resolves to nil.
    [[NSFileManager defaultManager] ubiquityIdentityToken]

Setup Screenshots

The iCloud Capability is setup.

enter image description here

The App ID has iCloud turned on.

enter image description here

My provisioning profile says it's setup for iCloud. I also revoked and reauthorized when I switched over to my Mac Mini.

enter image description here

I saw a post about Xcode 6 beta not having entitlements setup correctly, however near as I can tell this is ok. I did try changing the Ubiquity Container Identifier to $(TeamIdentifierPrefix)$(CFBundleIdentifier) but it didn't seem to make a difference.

enter image description here

Code

Maybe the problem is with my code? The only new part below is NSPersistentStoreUbiquitousContentNameKey: @"iCloudStore".

NSURL *url = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory
                                                     inDomains:NSUserDomainMask] firstObject];
url = [url URLByAppendingPathComponent:DATABASE_DOCUMENT_NAME];
self.document = [[UIManagedDocument alloc] initWithFileURL:url];

//  To cause light-weight model version migration to occur.
self.document.persistentStoreOptions = @{
                                         NSMigratePersistentStoresAutomaticallyOption : @YES,
                                         NSInferMappingModelAutomaticallyOption : @YES,
                                         NSPersistentStoreUbiquitousContentNameKey: @"iCloudStore"
                                         };

After the code above it's either performing openWithCompletionHandler or saveToURL:url forSaveOperation:UIDocumentSaveForCreating. I am also registered to be notified about NSPersistentStoreCoordinatorStoresDidChangeNotification and that is happening.

Kurt Anderson
  • 932
  • 1
  • 10
  • 26
  • 1
    Are you logged into iCloud on the simulator? And did you run the app on a device or just the simulator? – NickEntin Aug 23 '15 at 03:49
  • Running on my iPad does bring up the iCloud Debug Gauge. Thanks for that. I had made sure my Macs were signed into iCloud, but not the simulator itself. I signed into a simulator's settings so it linked up to my iCloud. The ubiquityIdentityToken isn't nil anymore, but on the simulator I'm still not seeing the iCloud Debug Gauge. – Kurt Anderson Aug 23 '15 at 04:09
  • Even though iCloud Debug Gauge isn't appearing for my simulator, the simulator's iCloud is working because my iPad is getting updates made on the simulator. Thanks for your help! – Kurt Anderson Aug 23 '15 at 04:14

1 Answers1

1

It's possible that the iCloud Debug Gauge does not work for apps running in the simulator because it doesn't have full functionality. This is from 2014, so things may have changed.

However, iCloud Core Data is not fully supported in the simulator. When testing across devices and the simulator, it seems that iCloud Core Data on the simulator only uploads changes, and never pulls them back down. This is still a big improvement over needing separate test devices, and a nice convenience, but iCloud Core Data support on the iOS Simulator is definitely not fully baked yet.

http://www.objc.io/issues/10-syncing-data/icloud-core-data/

The only thing I can find about it in official documentation says:

It is especially important to test an iCloud-enabled iOS app on an actual device. iOS Simulator doesn’t accurately simulate the use of iCloud under real-world conditions.

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

NickEntin
  • 431
  • 4
  • 16