Search code examples
iphoneobjective-ccocoa-touchnsnotificationsnsdocument

UIManagedDocument CompletionHandler & NSNotification


I am using Core Data and was wondering if I am doing things correctly. I am opening my UIManagedDocument from a singleton object using the completion handler and block below.

[[self managedDocument] openWithCompletionHandler:^(BOOL success) {
            if(success) {
                NSLog(@"DOCUMENT: Success, Opened ...");
                // TODO: Things to do when open.
                // ...
                // ...
            }
}];

On my UIViewController I have setup an observer to watch for a UIDocumentStateChangedNotification to indicate that I can start working with the document.

- (void)awakeFromNib {
    NSLog(@"%s", __PRETTY_FUNCTION__);
    NSNotificationCenter *notificationCenter = [NSNotificationCenter defaultCenter];
    [notificationCenter addObserver:self selector:@selector(documentIsReady) name:UIDocumentStateChangedNotification object:nil];
}

This seams to work just fine, but I am conscious that I am not using the callback block. One solution might be to create my own notification and post that from the block, it does the same thing essentially but just makes the code more obvious to read. Any comments would be much appreciated.


Solution

  • I'd say that if you only need to notify one controller, once, and only when the document is opened (you have an app that uses a single UIManagedDocument that gets passed between controllers, like the CS193P demo), it would be better to leave only the code inside the completion block.

    However, if your app is going to open and close the document many times, and multiple controllers have to be aware of that change, you should use notifications.