1

I've been reading a lot of CoreData+iCloud and understand its anything but straightforward. However, I'm getting stuck very very early and was curious if any of you had any idea.

My workbench: Xcode6.1.1, Swift, CoreData with added iCloud entitlements (testing on iOS8.1 Simulator as well as iOS8.1 device)

To see for yourself, simply start a new "Singe View Application" project in Xcode, check the "Use Core Data" checkbox. To enable iCloud, I added

let storeOptions = [
    NSPersistentStoreUbiquitousContentNameKey: "iCloudTest"
]

before invoking coordinator!.addPersistentStoreWithType(NSSQLiteStoreType, configuration:URL:options:error:)

I also added observers for NSPersistentStoreCoordinatorStoresWillChangeNotification, NSPersistentStoreCoordinatorStoresDidChangeNotification, and NSPersistentStoreDidImportUbiquitousContentChangesNotification.

When I run the application the very first time, my console output looks like this:

storesDidChange: NSConcreteNotification 0x15d6b890 {name = NSPersistentStoreCoordinatorStoresDidChangeNotification; object = <NSPersistentStoreCoordinator: 0x15e96380>; userInfo = {
added =     (
    "<NSSQLCore: 0x15e9a1b0> (URL: file:///var/mobile/Containers/Data/Application/F2E684E6-6B7B-4429-B9CD-24C45DE18D27/Documents/CoreDataUbiquitySupport/mobile~BB56F5F3-5448-41A3-A142-921D99A85855/iCloudTest/1660860A-83E1-4906-BEB8-DAF02DAC62B0/store/iCloudTest.sqlite)"
     );
}}
2015-01-16 22:17:51.367 iCloudTest[783:414835] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  mobile~BB56F5F3-5448-41A3-A142-921D99A85855:iCloudTest
Using local storage: 1
managedObjectContext: Optional(<NSManagedObjectContext: 0x15d69340>)
2015-01-16 22:17:51.815 iCloudTest[783:414857] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  mobile~BB56F5F3-5448-41A3-A142-921D99A85855:iCloudTest
Using local storage: 0

As stated in the Checkpoints of the Design for iCloud Programming Guide, I'm supposed to see Using local store: 1 and then shortly after Using local storage: 0 (in a matter of 1-5secs). So far so good. I'm happy.

Now, without changing one line of code, simply re-running the app, causes the following log output:

 storesDidChange: NSConcreteNotification 0x166606f0 {name = NSPersistentStoreCoordinatorStoresDidChangeNotification; object = <NSPersistentStoreCoordinator: 0x165485b0>; userInfo = {
added =     (
    "<NSSQLCore: 0x1658dfb0> (URL: file:///var/mobile/Containers/Data/Application/A742A53E-6785-40AC-A21A-16D4BB5F332E/Documents/CoreDataUbiquitySupport/mobile~9CB93B5A-F976-4AB8-B1B3-290F18DE796A/iCloudTest/0A12A44B-599B-49DD-8AD8-546EFD4E26DC/store/iCloudTest.sqlite)"
    );
}}
2015-01-16 22:08:55.133 iCloudTest[770:412842] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  mobile~9CB93B5A-F976-4AB8-B1B3-290F18DE796A:iCloudTest
Using local storage: 1
managedObjectContext: Optional(<NSManagedObjectContext: 0x1658f2e0>)
2015-01-16 22:11:29.005 iCloudTest[770:412855] -[PFUbiquitySafeSaveFile waitForFileToDownload:](489): CoreData: Ubiquity:  <PFUbiquityBaseline: 0x165a5950>(0)
    permanentLocation: <PFUbiquityLocation: 0x165a6690>: /var/mobile/Library/Mobile Documents/iCloud~com~mycompany~iCloudTest/CoreData/iCloudTest/.baseline/iCloudTest/47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=/baseline.zip
    safeLocation: <PFUbiquityLocation: 0x165a5ac0>: /var/mobile/Library/Mobile Documents/iCloud~com~mycompany~iCloudTest/CoreData/iCloudTest/.baseline/iCloudTest/47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=/mobile~9CB93B5A-F976-4AB8-B1B3-290F18DE796A
    currentLocation: <PFUbiquityLocation: 0x165a6690>: /var/mobile/Library/Mobile Documents/iCloud~com~mycompany~iCloudTest/CoreData/iCloudTest/.baseline/iCloudTest/47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=/baseline.zip

    storeName: iCloudTest
    modelVersionHash: 47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=
    baselineArchiveLocation: <PFUbiquityLocation: 0x165a6690>: /var/mobile/Library/Mobile Documents/iCloud~com~mycompany~iCloudTest/CoreData/iCloudTest/.baseline/iCloudTest/47DEQpj8HBSa~_TImW~5JCeuQeRkm5NMpJWZG3hSuFU=/baseline.zip

Download failed for file, error: Error Domain=NSCocoaErrorDomain Code=512 "The file download timed out." UserInfo=0x16670090 {NSLocalizedDescription=The file download timed out.}
2015-01-16 22:11:29.009 iCloudTest[770:412855] -[PFUbiquitySwitchboardEntryMetadata setUseLocalStorage:](808): CoreData: Ubiquity:  mobile~9CB93B5A-F976-4AB8-B1B3-290F18DE796A:iCloudTest
Using local storage: 0

Suddenly, iCloud is acting up! I timed about 90s between Using local storage: 1 and Using local storage 0.

Is this something to be worried about? I'm afraid if sync'ing takes that long for my users, it will be quite a nasty experience (just think using an app for 1min+ and suddenly iCloud content pops in...) I suppose since Using local storage 0 does eventually fire, iCloud sync is enabled? However, iCloud Report in the Debug navigator shows Status: iCloud not configured.

Am I doing something wrong?

PS: This is straight up apple boilerplate with only additional code to add iCloud entitlements.

opfeffer
  • 603
  • 5
  • 19

1 Answers1

0

It is nothing to worry about. Synchronizing with iCloud is inherently asynchronous and failure prone (like you are in airplane mode). Eventually the local and iCloud version will sync up and you'll need to handle some cases where identical records are in conflict.

GoZoner
  • 67,920
  • 20
  • 95
  • 145