Checking if NSFetchedResultsController returns no objects
I want to check if a NSFetchedResultsController isn't returning any objects. The point of this is that I want to conditionally display some开发者_如何转开发 information on the page to inform the user that there are no objects. (And also give them an option to create one.)
I have this connected to a normal table view. My thought was that I could check this in an if statement like this:
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
if ([[[_fetchedResultsController sections] objectAtIndex:section] numberOfObjects] == 0) {
return @"Message goes here.";
}
return nil;
}
This works fine, but I'm not sure if it is the best way to go about this. Also, the method is taking the section info as an argument, so I'm kind of restricted to using this only in certain areas. If I am not using a sectionNameKeyPath in my fetch request, would I be able to do this:
if ([[[_fetchedResultsController sections] objectAtIndex:0] numberOfObjects] == 0)
and just use 0 in place of section? I've check out this post, but the answer doesn't really seem to answer the question.
If your main interest is in whether zero objects are returned (vs. more than zero), then -[NSFetchedResultsController fetchedObjects] looks like it will provide the information (provided performFetch has been called).
精彩评论