UITableView not showing results until quit and reopen

I am using an NSFetchedResultsController to manage a UITableViewList of Gift objects. The idea is to let the user add Gifts by clicking the add button on the navbar. The add button should push a view for configuring the Gift onto the navigationController stack. Then, if the user actually enters data into the field, it should save the Gift so that if the user pops the view (i.e. navigates back to the list), he should see his new Gift in the list. Simple enough. The trouble is, that main list doesn't seem to update unless I reboot the app. Totally lost.

Called when the user clicks the "+" button on the main tableview (giftlistcontroller.m)

-(void) addgift{
    Gift *newGift = [NSEntityDescription insertNewObjectForEntityForName:@"Gift" inManagedObjectContext:self.managedObjectContext];
    GiftEditController *editController = [[GiftEditController alloc] initWithStyle:UITableViewStyleGrouped];
    editController.gift = newGift;

    [self.navigationController pushViewController:editController animated:YES];
    [editController release];
}

Then, in GiftEditController, if the user updates a field...

- (void)textFieldDidEndEditing:(UITextField *)textField {
    if (textField == nameField) {
        gift.name = textField.text;
    } else if (textField == priceField) {
        gift.price = [NSNumber numberWithFloat:[textField.text floatValue]];
    } 

    NSError *error;
    if (![gift.managedObjectContext save:&error]) {
        NSLog(@"Couldn't save in textFieldDidEndEditing");
    }
}

All of the standard fetchedresultscontroller delegate methods are defined in giftlistcontroller. Totally stumped, and any suggestions would be GREATLY (!!!!) appreciated.

Answers


I dont see you reloading the table anywhere. Add [myTableView reloadData]; to your code to get the table to "refresh" without having to exit the app.


Thanks everyone for your help. I finally found the solution, and I thought it would be wise to post it for anyone else with this problem (a simple google search makes one think there are more than a few out there).

You were right, I needed to reload my table. But that wasn't enough. The problem was that I had just dropped in the boilerplate fetchedResultsController code that apple uses, which doesn't work in this case. In particular, the first few lines of fetchedResultsController read:

-(NSFetchedResultsController *)fetchedResultsController
    if (fetchedResultsController != nil)
    {
        return fetchedResultsController;
    }

    ....
}

The problem is, even if one refreshes the table, it is still using the old fetchedResultsController which is unaware of the new entry. I was able to fix this problem by updating the fetchedResultsController in viewDidAppear:

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];

    // Set the fetched results controller to nil in case we are coming back
    // from a view that added or removed entries.
    self.fetchedResultsController = nil;

    NSError *error = nil;
    if (![[self fetchedResultsController] performFetch:&error]) {
        /*
         Replace this implementation with code to handle the error appropriately.

         abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development. If it is not possible to recover from the error, display an alert panel that instructs the user to quit the application by pressing the Home button.
         */
        NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
        abort();
    }

    //reload the table to catch any changes
    [self.tableView reloadData];

}

Thank you James Webster and Louie. You guys are lifesavers.


Need Your Help

Local web storing of files using HTML5

javascript html5 google-gears offlineapps

I was fascinated by Google Gears and its potential use in online game development, particularly massive online game development. One could take the game resources and store them locally using