motionpotion
motionpotion

Reputation: 2736

UITableView Not Displaying Latest Data When Using NSFetchedResultsController

I have a UIViewController with a UITableView on it. I have set the delegate and datasource for the UITableView to be the UIViewController in IB and also in the viewDidLoad. I have implemented all the delegate methods as per the apple documentation for UITableView and NSFetchedResultsController and I am seeing the core data information being loaded into the UITableView when the app launches.

...
[NSFetchedResultsController deleteCacheWithName:@"Stuff"];

self.tableview.delegate = self;
self.tableview.dataSource = self;
self.fetchedResultsController.delegate = self;
self.tableview.allowsSelection = NO;
self.sNavBar.delegate = self;
...

There is also a modal view that appears when a UIButton is tapped. When I add a new item using the modal view and then dismiss the modal view the UITableView on the parent UIViewController does not refresh with the latest data.

If I kill the app and restart it then item I added gets loaded into the UITableView. So I can see that the NSFetchedResultsController is loading my data but doesn't load the newly added data while the app is running.

I have been trying for hours to get this hooked up properly but I cannot see the problem. I have also tried deleting the fetchedresultscontroller cache.

I added a method to perform the fetch for the fetchedresultscontroller and it gets called when the modal is dismissed and also when the pulldown refresh happens on UITableView.

- (void)loadRecordsFromCoreData {

    NSError *error;
    if (![[self fetchedResultsController] performFetch:&error]) {
    // Update to handle the error appropriately.
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);  // Fail
    }
}

When the modal view is dismissed I call the viewWillAppear method which should reload the table view but I think there is a race condition here where new item is not added yet when the modal view disappears. The above loadRecordsFromCoreData does get called at this point. So to test that I added a refresh pulldown to the UITableView but this also does not pull in the newly added item.

UPDATE 1:

When I save the new item on the modal view in code below I then raise a notification:

- (IBAction)saveData:(id)sender {

 if (![self.txtEvent.text isEqualToString:@""]) {


     [self.managedObjectContext performBlockAndWait:^{
        NSError *error = nil;
        BOOL saved = [self.managedObjectContext save:&error];
        if (!saved) {
            // do some real error handling
            NSLog(@"Could not save due to %@", error);
        }
        [[MVCoreDataController sharedInstance] saveMasterContext];
    }];


     [[NSNotificationCenter defaultCenter] postNotificationName:@"refresh" object:nil];
     [self dismissViewControllerAnimated:YES completion:^{
         [delegate modalWasDismissed:self];
     }];       

 }
 else
 {

     UIAlertView *cannotSaveAlert = [[UIAlertView alloc] initWithTitle:@"Uh oh..." message:@"Some Problem." delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
     [cannotSaveAlert show];

 }
}

This "refresh" selector then calls viewWillAppear on the parent view controller which calls loadRecordsFromCoreData and [self.tableview reloadData] but nothing new appears in the table.

In viewWillAppear on the parent view controller I have the following code:

-(void) viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self loadRecordsFromCoreData];
[self.tableview reloadData];
}

What am I doing wrong?

Upvotes: 1

Views: 416

Answers (1)

nhgrif
nhgrif

Reputation: 62052

[self.tableview reloadData];

Call the reloadData method on a tableView any time you want it to, well, reload its data.

You could even pass the tableView to the modal view you're displaying so that the table view could be refreshed from the modal view in its viewWillDisappear or viewDidDisappear.

Upvotes: 2

Related Questions