简体   繁体   中英

UITableView datasource methods called before database open

I am writing an application that uses a Core Data database to store data which I then want to display using a UITableView. I have everything working, but was a little curious if there is a way around one small point that is bugging me...

When the app runs I do the following:

  1. Create a NSManagedDocument
  2. Create a NSFetchedResultsController
  3. Open the Core Data database.

I am using ...

[[self testDatabase] openWithCompletionHandler:^(BOOL success) {
    if(success) {
        ...
    }
}];

to open the database but my problem is that by the time the block has executed the UITableView dataSource has already called -[TableViewController tableView:numberOfRowsInSection:] and returned rows=0

My solution so far has been to ignore this first "automatic" call and instead add a performFetch and a reloadData to the block that executes when the database is open.

[[self testDatabase] openWithCompletionHandler:^(BOOL success) {
    if(success) {
        [[self fetchedResultsController] performFetch:nil];
        [[self tableView] reloadData];
    }
}];

My question, is there a way to stop or delay that first call? or is there something I should add to -[TableViewController tableView:numberOfRowsInSection:] to manage that first call, or does it simply not matter and its fine as it is?

您可以尝试以tableView.dataSource开始tableView.dataSource并在fetch之后设置它。

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