简体   繁体   中英

UITableView numberOfRowsInSection called before Core Data completionHandler returns?

I am creating a simple application using Core Data (UIManagedDocument) along with a UITableView. Currently in the TableViewController I am checking if the database exists and either creating or opening as necessary using:

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. As a consequence when UITableView calls:

UITableView tableView:numberOfRowsInSection:

fetchedResultsController (where it gets the row count) is still null.

My understanding is that you have to have the document created/open to setup the fetchedResultsController as it needs a valid managedObjectContext. This is my first foray into Core Data so any help / information would be much appreciated.

...

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];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM