Database Reference
In-Depth Information
with iCloud turned off, and then the data be loaded into the new container.
To accomplish this, you would need to follow a procedure similar to the one
detailed in Section 6.5, Migrating an Existing Application , on page 113 .
6.5
Migrating an Existing Application
Because Core Data keeps track of changes via transaction logs, it's impossible
to just “turn on” iCloud in an existing application and expect all the data to
get pushed into the cloud. A few other steps are necessary.
Detecting a Migration
The first question when adding iCloud to an existing iOS application is whether
the migration is necessary. There are two key criteria for answering this
question.
• Is there any existing data to migrate?
• Has the migration already been performed?
Both of these questions can be answered easily if we do a simple filename
change. For example, if our application has always used a SQLite file named
PPRecipes.sqlite , then when we want to add iCloud integration to our application,
we should start using a filename of PPRecipes-iCloud.sqlite . A simple “does this
file exist?” check tells us whether we need to migrate our existing data.
If it is not possible or reasonable to rename the file, the fallback option is to
store a flag in the NSUserDefaults to let us know whether the migration has
occurred. This option is second best for a couple of reasons.
• As we will demonstrate in a moment, the file needs to be moved anyway.
NSUserDefaults classes tend to be a bit unreliable, especially during testing.
Assuming we are going to use a file rename strategy to determine whether a
migration is required, the first step is to look for the “ old” filename to determine
whether a migration is required. As part of this change to handle the migration,
we are going to refactor the NSPersistentStoreCoordinator initialization code some-
what to make it more maintainable with these additions.
iCloud/PPRecipes/PPRAppDelegate.m
dispatch_queue_t queue;
queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
NSMutableDictionary *options = [[NSMutableDictionary alloc] init];
[options setValue:[NSNumber numberWithBool:YES]
forKey:NSMigratePersistentStoresAutomaticallyOption];
[options setValue:[NSNumber numberWithBool:YES]
forKey:NSInferMappingModelAutomaticallyOption];
 
 
 
Search WWH ::




Custom Search