tableView reloadData does not work

2019-02-20 21:38发布

I have a tableview in a tab bar application.

I am loading the data in viewDidLoad

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

I am attempting to reload the data in viewWillAppear

- (void)viewWillAppear:(BOOL)animated {
    [self.tableView reloadData];
}

In this view, I call another view, which when changed, updated the core data when released. When I navigate back to the tableview, it does not update. The only way it will update, is if I quit the app and reload from scratch.

In total there are 2 separate table views. The strange thing is, the first tableview DOES refresh.

Any thoughts? Rob

2条回答
【Aperson】
2楼-- · 2019-02-20 21:58

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

查看更多
你好瞎i
3楼-- · 2019-02-20 22:01

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];
}
查看更多
登录 后发表回答