I’m using core data and I have a simple two-entity model. My navigation controller has an “add” button that allows the user to add new school years to the database… (i.e. freshman year, sophomore year, etc…)
When the user clicks the add button, I call this function which pushes my simple data entry form onto the stack. Works great. User adds new school year and clicks the “done” button on the form. I pop that form off the stack and the user comes back to the main screen.
My problem is that the main screen (tableview) does not show the new entry in the table. How do I make the code refresh the table? Note: If I exit the simulator and relaunch it, the new record is then visible.
Here is the function I call when the user clicks the “+” sign to add a new school year.
- (void)addSchoolYear {
vcAddYear *addYearViewController = [[vcAddYear alloc] initWithNibName:@"vcAddYear" bundle:nil];
[self.navigationController pushViewController:addYearViewController animated:YES];
[addYearViewController release];
}
When the user is done adding the new record and then clicks on the “Save” button. Here is that function.
- (void)SaveButton {
//get a pointer to the managedobjectcontext
GradetrackAppDelegate *AppDelegate = (GradetrackAppDelegate *)[[UIApplication sharedApplication] delegate];
managedObjectContext = AppDelegate.managedObjectContext;
Schoolyear *schoolyear = (Schoolyear *)[NSEntityDescription insertNewObjectForEntityForName:@"Schoolyear" inManagedObjectContext:managedObjectContext];
// plug the user's data into a schoolyear record
[schoolyear setNameForThisYear: _nameYear.text];
[schoolyear setSchoolName: _nameSchool.text];
NSError *error = nil;
if (![managedObjectContext save:&error]) {
// Handle the error.
}
//[AppDelegate.delegateSchoolyearsArray insertObject:schoolyear atIndex:0];
[self.navigationController popViewControllerAnimated:YES];
return;
}
At this point, my code returns to the calling rootviewcontroller which is a tableview and the table looks just like it did before the user added the new record. How do I tell my tableview to reload and show the new data item along with the other old ones?
Many thanks.
You could always use an
NSFetchedResultsController.Here’s the documentation. Implement the delegate methods and away you go.