2

Why doesn't "[self.tableView reloadData]" ensure I see the latest row for data I just added to a UITableView? (code included) That is after comign back from a AddItem controller to this delegate method I:

  • update the core data repository with the new value
  • call: "[self.tableView reloadData]"
  • dismiss the modal view
  • but then back on the UITableView the new row isn't there?
  • When I stop and restart the iPhone app in the simulator the row does appear correctly

Why doesn't "reloadData" work for me here, and what should I do?

Note I understand there is an approach where you manually create a new row via "insertRowsAtIndexPaths" in the tableview. Not sure which approach is best, but in any case would like to understnad why reloadData is not working here.

Code:

- (void)newItemController:(NewItemController *)controller didFinishWithSave:(BOOL)save newItemText:(NSString*)itemText 
{   
    // Add data to CoreData store
    WEView *newWEView = (WEView *) [NSEntityDescription insertNewObjectForEntityForName:@"WEView" inManagedObjectContext:managedObjectContext];
    newWEView.title = itemText;

    NSError *error = nil;
    if (![managedObjectContext save:&error]) {
        // Handle the error.
        DLog(@"ERROR DURING SAVE");
        abort();
    }

    // Dismiss the modal view to return to the main list
   [self dismissModalViewControllerAnimated:YES];

    // Reload Data
    [self.tableView reloadData];
}   

thanks

PS. This is the code I use to populate the tableview data instance variable:

- (void)updateTableDataViaCoreData 
{
    // Core Data Request
    NSFetchRequest *request = [[NSFetchRequest alloc] init];
    NSEntityDescription *entity = [NSEntityDescription entityForName:@"WEView" inManagedObjectContext:managedObjectContext];
    [request setEntity:entity];

    // CoreData Sort Descriptor
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:NO];
    NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
    [request setSortDescriptors:sortDescriptors];
    [sortDescriptors release];
    [sortDescriptor release];

    // Core Data - Execute Request
    NSError *error = nil;
    NSMutableArray *mutableFetchResults = [[managedObjectContext executeFetchRequest:request error:&error] mutableCopy];
    if (mutableFetchResults == nil) {
        // Handle the error.
        DLog(@"Error when fetching data");
        abort();
    }

    // Core Data finish Up
    [self setViewConfigArray:mutableFetchResults];
    [mutableFetchResults release];
    [request release];
}
WrightsCS
  • 50,551
  • 22
  • 134
  • 186
Greg
  • 34,042
  • 79
  • 253
  • 454

3 Answers3

4

From your code sample, it looks like you've used the recommended Core Data method where you have a NSFetchedResultsController managing the tableview's data. If that is the case, then you need to tell the tableview's fetchedresultscontroller to re-fetch the data before calling reloadData.

So before reloadData, tell your NSFetchedResultsController to re-fetch the data

[[self frc] performFetch:&error];

Inserting the object into the managedcontext doesn't make it available to the tableview.

ferdil
  • 1,259
  • 11
  • 24
  • thanks ferdil - this worked after refetching - the only question I have would be that in my case I don't have any instance variable that represents the fetch, so I basically have to call all the code again to get all rows from the core data store - does this sound right or is there a less heavy handed way to do it? I've posted in the original post the code I'm currently using to update the list view data, so now this method is called from viewDidLoad + the update row method. – Greg Mar 31 '11 at 09:47
  • Greg - there is a way to do this less heavy-handed, but it gets real complex. You then need to implement a bunch of delegates that let your tableView be updated (beginUpdates/endUpdates) I used the sample "Recipes" to learn how to use CoreData and fetch requests – ferdil Apr 08 '11 at 09:45
0

Try to reload data before dismissing the view controller

Ruben Marin
  • 1,639
  • 14
  • 24
0

You had to call the table reload with as

[classname.tableview reloadData];

Hope it helps.....

Aman Aggarwal
  • 3,754
  • 1
  • 19
  • 26