Database Reference
In-Depth Information
The first part of this code should be familiar. We create an NSMutableDictionary
and add the options both to infer a mapping model and to attempt a migration
automatically. From here, though, we are in new territory. -URLForUbiquityCon-
tainerIdentifier: is a new addition to the NSFileManager that came with iCloud. This
call requests a URL used to store information that iCloud is going to use to
sync the NSPersistentStore . If iCloud is disabled on this device (or Mac OS X
computer), this call will return nil . Once we have the URL and have established
that it is not nil , we know iCloud is available, and we can begin to configure
it.
The URL we receive points to a file path; it looks something like this:
file://localhost/private/var/mobile/Library/
Mobile%20Documents/K7T84T27W4~com~pragprog~PPRecipes/
Notice this file path is outside our application sandbox. Even so, we have
some control over what goes into this directory. For example, if we use a
document-based application, we could append the name of the document
onto this path so that each document is kept separate. In our current example,
however, we are going to create a single subdirectory for our Core Data stack.
This will help us in the future if we decide to make changes or sync additional
items. Once the URL is defined, we need to add it to our options dictionary
with the key NSPersistentStoreUbiquitousContentURLKey .
In addition to the URL for the storage location, we must tell iCloud what data
it should be syncing with. If we have a single application shared between
iPhone and iPad, as in our current example, we can use the bundle identifier
as a unique key to define what data is to be shared across the devices.
However, if we are also sharing data with a desktop application, the bundle
identifier may not be appropriate. The data identifier is stored in the options
dictionary with the key NSPersistentStoreUbiquitousContentNameKey .
Once again, the addition of those two keys is the bare minimum required to
enable iCloud for an iOS application. With that information, the operating
system creates a directory for the content, downloads any content that exists
in the cloud, and starts syncing that data for us. However, that initial down-
load (or for that matter subsequent syncing) can take an indeterminate amount
of time. If there is nothing currently in the store, the creation of the directory
structure will be virtually instantaneous. However, if there is data to download,
it could take some time, depending on the speed of the network connection
and the amount of data. As before, we must change how we add the NSPersis-
tentStore to the NSPersistentStoreCoordinator .
 
 
Search WWH ::




Custom Search