Reputation: 1506
I want to sync my Core Data shoebox app with iCloud. An example project Apple provides shows how to set up the NSPersistentStoreCoordinator to enable iCloud. The code roughly looks like this:
persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel: [self managedObjectModel]];
NSDictionary* options = [NSDictionary dictionaryWithObjectsAndKeys:@"com.company.app", NSPersistentStoreUbiquitousContentNameKey, cloudURL, NSPersistentStoreUbiquitousContentURLKey,nil];
[psc addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeUrl options:options error:&error]);
The problem is: if there's data already stored in iCloud, this method takes forever to return the persistentStoreCoordinator. If I do it synchronously I'm watching a blank screen for a few minutes, if I do it asynchronously my app is completely unusable because I can't interact with Core Data.
Surely there's a way I could add the persistent store as I would without iCloud, and then let iCloud work asynchronously?
Basically, my question is, what is iCloud doing during the time it takes to initially sync and can I have my persistentStoreCoordinator first, and let the data sync in the background?
Upvotes: 4
Views: 2178
Reputation: 3137
When you use iCloud to sync Core Data you need to load the store on a separate thread. Take a look at:
https://devforums.apple.com/thread/126670
That demonstrates asynchronous loading of the store.
Upvotes: 2