gpt4 book ai didi

iphone - UIManagedDocument CompletionHandler 和 NSNotification

转载 作者:行者123 更新时间:2023-12-03 21:03:05 26 4
gpt4 key购买 nike

我正在使用 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com