Database Reference
In-Depth Information
Now we come to the more complicated decision. iCloud is enabled, but we
don't know whether a migration is needed. First, we go ahead and complete
the storeURL with the “new” filename, PPRecipes-iCloud.sqlite . Next, we construct
the “old” file URL for PPRecipes.sqlite . If the “old” URL exists (via the NSFileManager ),
then we need to perform a migration.
We add the “old” file to the NSPersistentStoreCoordinator and obtain a reference to
the NSPersistentStore . Once we confirm that it was loaded successfully, we can
proceed with the migration.
Since we want the “new” store to be connected to iCloud, we now need to add
in the options for iCloud configuration to our options dictionary. These are
the options we discussed in Configuring iCloud , on page 106 . Once the options
dictionary has been updated, we can kick off the migration via a call to
-migratePersistentStore:toURL:options:withType:error: . This call does several things:
• Creates a new SQLite file at the location specified by storeURL
• Copies all the data from the “old” file to the “new” file
• Registers the “new” file with iCloud per the options specified in the
dictionary
• Removes the “old” store from the NSPersistentStoreCoordinator
• Adds the “new” SQLite file to the NSPersistentStoreCoordinator
It's a lot of work for one line of code, and it should be noted that this line of
code can take some time. Therefore, depending on our user experience, we
may want to broadcast a notification before the work begins so our user
interface updates and lets the user know what's going on.
Assuming the migration was successful, we now need to delete the old SQLite
file from disk so we do not accidentally repeat these steps on the next launch.
Once the migration and the deletion are complete, we are finally ready to
notify the UIApplicationDelegate that the Core Data stack is ready for use.
6.6
Desktop iCloud Integration
So far in this chapter we have focused primarily on iOS; the reason is that
the desktop implementation is actually a subset of the iOS implementation.
There is no UIManagedDocument on the desktop. As a result, we must use the
traditional Core Data stack and add the options to the NSPersistentStoreCoordinator
ourselves. Even if we are using an NSPersistentDocument , we must still handle
adding the options for the NSPersistentStoreCoordinator .
 
 
Search WWH ::




Custom Search