I have a simple Core-Data
based application which is a UITableView
populated by the user filling in some UITextFields
, with the UITableView getting updated via NSFetchedResultsController
.
As an iOS 7 only app and right now in the App Store without iCloud
enabled, it's working really well. However, the time has come to introduce iCloud.
I have spent a long time reading iOS 7 specifics with iCloud and Core Data, including the WWDC videos from 2013 relating to this very topic. I've also read the excellent http://www.objc.io/issue-10/icloud-core-data.html page with reference to the easier methods. I've read this excellent post CoreData and iCloud and followed the Git code, but I'm confused.
Problem
My issue right now is not a problem; it's where to begin. Unfortunately, as good as all of the posts are, they don't tailor to newbies in this field.
I have a few questions that are preventing me from even starting with entire reference to: http://www.objc.io/issue-10/icloud-core-data.html .
1) The code that he has pasted in there with the notifications and the iCloud Support pragma marks, do I put that both in the App Delegate?
2) Where the comment states, use these options in your call to -addPersistentStore
. What do I do with that? I can see that that is called from the persistentStoreCoorindator, but what exact options do I put in there for simple synching?
3) Where does the iCloudPersistentStoreOptions
method get called from?
4) Do I have to change anything in the ManagedObjectContext
method or the PersistentStoreCoordinator
method?
5) Do I have to have a notification UITableView
that refreshes the UI?
I've read so many posts on iOS 5 and iOS 6 articles and what I want is:
App with data updates to the new version with iCloud enabled, uploads information to the iCloud and other devices with the same iCloud account, downloading that data.
Update
I have spent some time figuring this out yesterday and I think I'm almost there. After watching the WWDC videos again, I've figured out the answers to question 1, 2 and 3 earlier, but I have an issue whereby the App Store version of my app (without iCloud) has some data and when I upload to the new development build with iCloud, all of my data is not appearing in the UI (though it does exist in Core Data because I'm seeing a core data backup in the settings on the device).
I achieved this with the following persistentStoreCoordinator code:
- (NSPersistentStoreCoordinator *)persistentStoreCoordinator
{
if (_persistentStoreCoordinator != nil) {
return _persistentStoreCoordinator;
}
NSURL *storeURL = [[self applicationDocumentsDirectory] URLByAppendingPathComponent:@"Planner.sqlite"];
NSFileManager *fm = [NSFileManager defaultManager];
NSURL *url = [fm URLForUbiquityContainerIdentifier:nil];
NSError *error = nil;
_persistentStoreCoordinator = [[NSPersistentStoreCoordinator alloc] initWithManagedObjectModel:[self managedObjectModel]];
NSDictionary *options = @{
NSMigratePersistentStoresAutomaticallyOption : @YES,
NSInferMappingModelAutomaticallyOption : @YES,
NSPersistentStoreUbiquitousContentNameKey : @"Planner"
};
if (![_persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:options error:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
return _persistentStoreCoordinator;
}
I'm stuck on question 4 and 5 above though. Do I have to change anything else in the persistentStoreCoordinator and do I have to change anything in the NSManagedObjectContext? Finally, question 5 - do I have to change a notification in the actual TableView, because the data isn't displaying and I suspect that it's currently just not updating it's UI..
Any guidance on this question would be really appreciated.
mergePolicy
on the context. NSFetchedResultsController
delegate methods for inserting/deleting/moving rows.mergeChangesFromContextDidSaveNotification:
method on the queue of your context (e.g. main queue if your using a main thread context) when you receive the NSPersistentStoreDidImportUbiquitousContentChangesNotification
notification.