Database Reference
In-Depth Information
void (^completion)(BOOL) = ^(BOOL success) {
if (!success) {
ALog(@ "Error saving %@\n%@" , storeURL, [document debugDescription]);
return ;
}
dispatch_queue_t mainQueue;
mainQueue = dispatch_get_main_queue();
dispatch_sync(mainQueue, ^{
[self contextInitialized];
});
};
if ([[NSFileManager defaultManager] fileExistsAtPath:[storeURL path]]) {
[document openWithCompletionHandler:completion];
return ;
}
[document saveToURL:storeURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:completion];
[self setManagedDocument:document];
});
Constructing the UIManagedDocument is a case of calling +alloc and then -initWith-
FileURL: and passing in the storeURL that we previously constructed. Once the
UIManagedDocument is initialized, we can set the options for the NSPersistentStore
via a call to -setPersistentStoreOptions: . Note that we do not have the ability to add
more than one NSPersistentStore to a UIManagedDocument .
We also want to take this opportunity to set the merge policy for the UIManaged-
Document . This setting is actually performed on the NSManagedObjectContext
directly.
Unlike when we construct a straight Core Data stack, though, the initialization
of the UIManagedDocument is not the end for us. We must now save it. We could
save it later, but it is best to put all of this initialization code in the same
place rather than have it spread out across our UIApplicationDelegate . To save
the UIManagedDocument , we must first discover if it already exists; based on that
information, we can call the appropriate method.
Whether the UIManagedDocument existed before, the process is the same: we call
a method on the UIManagedDocument and pass in a completion handler. Since
that completion handler is the same no matter which method we call, we
construct the completion handler first and then determine which method to
call.
 
 
Search WWH ::




Custom Search