I use this code throughout my project to update the title of my UIBarButtonItem.
In this particular class, I have an NSFetchedResultsController which I want to to get the count for to set as the button’s title.
I am calling this code in ViwDidLoad but it seems as though it is being called before the fetch, therefore the count is always 0. I tried putting it at the end of the Fetch delegate method, but that didn’t help either. Any ideas? When I NSLog the count in the fetch delegate method, it is correct.
self.myBarButton.title = [NSString stringWithFormat: @"%i count", [fetchedResultsController.fetchedObjects count]];
viewDidLoad
- (void)viewDidLoad
{
[super viewDidLoad];
if (managedObjectContext == nil)
{
self.managedObjectContext = [(TestAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}
NSSet *filteredFromSession=[selectedSession.property filteredSetUsingPredicate:[NSPredicate predicateWithFormat: @"name == %@",name]];
if ([filteredFromSession count] > 0)
{
self.exercise = [filteredExercisesFromSession anyObject];
}
self.setLabel.title = [NSString stringWithFormat: @"%i sets", [fetchedResultsController.fetchedObjects count]];
}
It’s most probable that at the time you do this,
fetchedResultsControllerisnil.If you’ve a getter method defined for
fetchedResultsControllerand have declared it as a property, I suggest you do the fetch before your line above,Basically ensure that the fetch is completed prior to setting the title. Once you do this, you might want to remove the fetch from the place you’re doing right now.