I have a tableview in a tab bar application.
I am loading the data in viewDidLoad
managedObjectContext = nil;
managedObjectContext = [(R
You need to do what you do in viewDidLoad
in viewWillAppear
. viewDidLoad
is only called at startup, so despite changing the data, your view never changes its own local data, so when you call reloadData
it still uses the old data.
Edit:
What you should have:
- (void)viewWillAppear:(BOOL)animated {
managedObjectContext = nil;
managedObjectContext = [(RecipesAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"BrilliantMustache" inManagedObjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"badge.length > 0"];
[fetchRequest setPredicate:predicate];
NSSortDescriptor *sortDescriptor1 = [[NSSortDescriptor alloc] initWithKey:@"badge" ascending:YES];
NSSortDescriptor *sortDescriptor2 = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor1,sortDescriptor2, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"badge" cacheName:nil];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptor1 release];
[sortDescriptor2 release];
[sortDescriptors release];
[self.tableView reloadData];
[super viewWillAppear:animated];
}
- (void)viewDidLoad:(BOOL)animated {
[super viewDidLoad:animated];
}
Have you implemented the NSFetchedResultsControllerDelegate
methods properly? The FRC should be handling updating the table when it receives a notification of changes from the managed object context...
Edit: NSFetchedResultsController Class Reference