NSFetchedResultsController:在调用 -performFetch 之前我应该始终检查 fetchedObjects==nil 吗?
我正在使用 NSFetchedResultsController 作为我的表视图。我在控制器的 -viewDidLoad 方法中调用 -performFetch 。
有时我的控制器会被卸载然后重新加载,从而导致再次调用 -viewDidLoad 和 -performFetch。我发现这导致了错误:“NSFetchedResultsController 错误:在控制器中找不到部分‘(null)’”。我发现像这样多次调用 -performFetch 会导致问题,并修改了我的 -viewDidLoad: 方法来执行以下操作:
if( fetchedResCtrlr.fetchedObjects == nil )
{
NSError *error;
if ( ![fetchedResCtrlr performFetch:&error] )
...
}
作为 Core Data 的新手,我想知道这是否是正确的操作。我真的应该能够多次调用 -performFetch: 而不会出现错误吗?我应该在 -viewDidUnload: 中做一些事情吗?
谢谢!
I'm using NSFetchedResultsController for my table view. I call -performFetch inside my controller's -viewDidLoad method.
Sometimes my controller gets unloaded and then re-loaded, resulting in another call to -viewDidLoad and -performFetch. I found that this was causing an error: "NSFetchedResultsController error: section '(null)' not found in controller". I found that calling -performFetch multiple times like this was causing the problem, and modified my -viewDidLoad: method to do the following:
if( fetchedResCtrlr.fetchedObjects == nil )
{
NSError *error;
if ( ![fetchedResCtrlr performFetch:&error] )
...
}
Being new to Core Data, I'm wondering if this is the correct action to take. Should I actually be able to call -performFetch: more than once without error? Should I be doing something in -viewDidUnload:?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通常,不需要对
-viewDidUnload:
中的NSFetchedResultsController
执行任何操作。另外,根据-fetchedObjects
检查 nil 通常是不值得的。听起来您的代码还有其他流程问题。多次调用-performFetch:
只会损害性能,而不会产生任何其他不良影响。Normally, there is nothing that needs to be done with the
NSFetchedResultsController
in the-viewDidUnload:
. Also, checking for nil against the-fetchedObjects
is normally not worth it. Sounds like your code has other flow issues. Calling-performFetch:
more than once would only harm performance on its own, without any other ill effects.