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 there, if iCloud is enabled, we need to add the iCloud
URL to our options dictionary. However, we do not want our document stored
at the “root” of our iCloud sandbox. Rather, we want to create a directory
under the root with the same name as the document we are creating locally.
Therefore, we are going to append “PPRecipes” to the end of the URL. 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 need to tell iCloud what
data it is going to sync. 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 .
The addition of these 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 begins syncing the data. However, as with the URL call, the initial
download (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. But if there is data to
download, it could take some time, depending on the speed of the network
connection and the amount of data. Therefore, the application needs to be
able to handle a delay in the creation of the persistent store. There are many
ways to deal with this delay, and that is an exercise left to the user experience
experts.
Building the UIManagedDocument
Once the options dictionary has been constructed, it's time to build the
UIManagedDocument . The construction of the document itself is short.
iCloud/PPRecipes/PPRAppDelegate.m
UIManagedDocument *document = nil;
document = [[UIManagedDocument alloc] initWithFileURL:storeURL];
[document setPersistentStoreOptions:options];
NSMergePolicy *policy = [[NSMergePolicy alloc] initWithMergeType:
NSMergeByPropertyObjectTrumpMergePolicyType];
[[document managedObjectContext] setMergePolicy:policy];
 
 
 
Search WWH ::




Custom Search