iphone - UIManagedDocument CompletionHandler 和 NSNotification

标签 iphone objective-c cocoa-touch nsnotifications nsdocument

我正在使用 Core Data,并且想知道我的做法是否正确。我正在使用下面的完成处理程序和 block 从单例对象打开我的 UIManagedDocument

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

在我的 UIViewController 上,我设置了一个观察者来监视 UIDocumentStateChangedNotification 以指示我可以开始使用该文档。

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

这看起来工作得很好,但我意识到我没有使用回调 block 。一种解决方案可能是创建我自己的通知并从 block 中发布该通知,它本质上执行相同的操作,但只是使代码更易于阅读。如有任何意见,我们将不胜感激。

最佳答案

我想说,如果您只需要通知一个 Controller 一次,并且仅在打开文档时(您有一个应用程序使用在 Controller 之间传递的单个 UIManagedDocument,如 CS193P 演示),那么它会最好只保留完成 block 内的代码。

但是,如果您的应用要多次打开和关闭文档,并且多个 Controller 必须了解该更改,则您应该使用通知。

关于iphone - UIManagedDocument CompletionHandler 和 NSNotification,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12588547/

相关文章:

iphone - UITableView自定义纯灰色索引

iphone - 将 delegate 设置为 nil 到 "avoid message sent to deallocated instance"

ios - iPad 应用程序在横向左侧而不是横向右侧打开时添加边距

ios - 从 Core Data 中将一对多显示为一对一

ios - 使用 CheckmarkAccessory 和单元格重用标识符选择 UITableViewCells

objective-c - 适用于非英语语言的 iPhone 应用程序

objective-c - 使用 SLComposeViewController 发布到 Twitter 在 iOS 8 设备上不工作

ios - 一旦应用程序在收到推送通知后变为事件状态,就为用户呈现 UIAlertView

iphone - “NSString”可能无法响应 '-hexIntValue' 警告,功能正常

iphone - 如何在 iTune Connect 上为我的 iPhone 应用程序设置自动续订应用程序内购买?