[英]UITableView numberOfRowsInSection called before Core Data completionHandler returns?
I am creating a simple application using Core Data (UIManagedDocument) along with a UITableView. 我正在使用Core Data(UIManagedDocument)和UITableView创建一个简单的应用程序。 Currently in the TableViewController I am checking if the database exists and either creating or opening as necessary using:
当前在TableViewController中,我正在检查数据库是否存在,并根据需要使用以下方法创建或打开:
UIDocument saveToURL:forSaveOperation:completionHandler:
OR
UIDocument openWithCompletionHandler:
The problem I am having is that my UITableView datasource methods are getting called before the completion handler (which sets up the fetchedResultsController) returns. 我遇到的问题是,在完成处理程序(设置fetchedResultsController)返回之前,将调用UITableView数据源方法。 As a consequence when UITableView calls:
结果,当UITableView调用时:
UITableView tableView:numberOfRowsInSection:
fetchedResultsController (where it gets the row count) is still null. fetchedResultsController(获取行计数的位置)仍然为null。
My understanding is that you have to have the document created/open to setup the fetchedResultsController as it needs a valid managedObjectContext. 我的理解是,必须创建/打开文档才能设置fetchedResultsController,因为它需要有效的managedObjectContext。 This is my first foray into Core Data so any help / information would be much appreciated.
这是我对Core Data的首次尝试,因此,非常感谢您的帮助/信息。
... ...
EDIT : I have looked at this a bit more, and although I have not figured it out yet I think its just a case of moving things around and verifying that things happen in a more correct order. 编辑 :我已经看了更多,尽管我还没有弄清楚,但我认为这只是移动事物并验证事物以更正确的顺序发生的一种情况。
Your table delegate method could do this: 您的表委托方法可以做到这一点:
numberOfRowsInSection {
if ( myFetchedResultsController ) { return <WhateverYouNormallyDoHere>; }
return 0;
}
and then in your completion handler, just refresh the table 然后在完成处理程序中,刷新表
[myTableView reloadData];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.