Database Reference
In-Depth Information
6.2
Direct NSManagedObjectContext to iCloud
If you have been using Core Data for a while, you will feel right at home cre-
ating a Core Data stack; otherwise, this code will look similar to the stack we
discussed in Chapter 1, Under the Hood of Core Data , on page 1 . The code
to add iCloud to the Core Data stack is short, straightforward, and easy to
add. This is good and bad. It's good, in that it takes a small amount of effort
to add iCloud to your Core Data-based application, but it's bad because there
are not very many options to configure, and it's a one-size-fits-all design. If
your data model is complex or if you have elements that you do not want to
sync, then lack of configurability will cause some interesting solutions. For
example, if you have nonsyncable data, then you may need to split your data
into more than one persistent store. Another situation that can feel limiting
is if you have a very high churn rate in your data structures. iCloud prefers
to have an opportunity to process the data, and a high rate of content creation
or change can cause it to get backed up. In that situation, it may be necessary
to “roll up” your data changes to decrease the number of entities being created
or the frequency of saves. Reviewing your application' s activities in Instruments
(as discussed in Chapter 4, Performance Tuning , on page 61 ) will help you to
determine whether you have strayed off the golden path.
Configuring iCloud
To integrate iCloud with our Core Data stack, we insert some additional
options to the NSPersistentStore when we add the NSPersistentStore to the NSPersis-
tentStoreCoordinator .
iCloud/PPRecipes/PPRAppDelegate.m
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setValue:[NSNumber numberWithBool:YES]
forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setValue:[NSNumber numberWithBool:YES]
forKey:NSInferMappingModelAutomaticallyOption];
NSFileManager *fileManager = [NSFileManager defaultManager];
NSURL *cloudURL = [fileManager URLForUbiquityContainerIdentifier:nil];
if (cloudURL) {
DLog(@ "iCloud enabled: %@" , cloudURL);
cloudURL = [cloudURL URLByAppendingPathComponent:@ "PPRecipes" ];
[options setValue:[[NSBundle mainBundle] bundleIdentifier]
forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setValue:cloudURL
forKey:NSPersistentStoreUbiquitousContentURLKey];
} else {
DLog(@ "iCloud is not enabled" );
}
 
 
 
Search WWH ::




Custom Search