Databases Reference
In-Depth Information
In the completion handler, we check to see whether it completed successfully.
If it was not successful, we present an error to the user and perhaps try to
recover from the error. If the completion was successful, we want to notify
the AppDelegate that the UIManagedDocument has been initialized and that normal
program flow can resume.
With the completion block constructed, we can now ask the NSFileManager if
the file already exists; if it does, we call -openWithCompletionHandler: on the UIMan-
agedDocument . If it does not exist, we need to create it with a call to -saveToURL:
forSaveOperation:UIDocumentSaveForCreating: completionHandler: . If this seems overly
complicated, that's because it is. This really should be abstracted away into
the framework.
Observing Changes to the UIManagedDocument
Once our UIManagedDocument has been constructed, it can be quite useful to
know its current state. Since the UIManagedDocument saves on its own accord,
we won't automatically know whether it is clean or dirty. We need some kind
of callback system in place to notify us. Fortunately, the UIManagedDocument
does broadcast notifications when the state changes. By adding our UIApplica-
tionDelegate as an observer to the notification UIDocumentStateChangedNotification ,
we are notified of those changes and can act accordingly.
iCloud/PPRecipes/PPRAppDelegate.m
- ( void )contextInitialized;
{
DLog(@ "fired" );
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector: @selector (documentStateChanged:)
name:UIDocumentStateChangedNotification
object:[self managedDocument]];
There are several places that we could start observing this notification; placing
it in the -contextInitialized is a personal preference. It is possible to start listening
to it as part of the initialization of the UIManagedDocument , for example. When
this notification fires, we receive the UIManagedDocument as the object of the
notification. From the UIManagedDocument , we then respond accordingly.
iCloud/PPRecipes/PPRAppDelegate.m
- ( void )documentStateChanged:(NSNotification*)notification
{
switch ([[notification object] documentState]) {
case UIDocumentStateNormal:
DLog(@ "UIDocumentStateNormal" );
break ;
case UIDocumentStateClosed:
 
 
 
Search WWH ::




Custom Search