Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I manually need to save the managedObjectContext for NSFetchedResultsController if I change some attribute?

I'm using an NSFetchedResultsController to populate and manage my tables data source.

When a user picks a certain row, an actionsheet pops up and then lets the user change a value for that row:

NSManagedObject *managedObject = [fetchedResultsController objectAtIndexPath:selectedRowIndexPath];
[managedObject setValue:status forKey:@"status"];

This works really well and I can see the changes immediately in the tableview. This means that the NSFetchedResultsController knows something is changed and therefore reloads that tableviewcell. When I stop and quit my app (completely) then reopen it, the change is not saved.

I would think NSFetchedResultsConroller takes care of saving changes.

Do I need to save manually using the following code after every change?

// Save the context.
NSError *error = nil;
if (![self.managedObjectContext save:&error]) {
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    abort();
}

Or maybe call this code in:

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject 
       atIndexPath:(NSIndexPath *)indexPath 
     forChangeType:(NSFetchedResultsChangeType)type 
      newIndexPath:(NSIndexPath *)newIndexPath
like image 259
Pieter Avatar asked Mar 14 '12 12:03

Pieter


1 Answers

You are correct. You need to manually save the context to the store. NSFetchedResultsController grabs data from the context, but does not save data to the store.

like image 127
Gobot Avatar answered Sep 19 '22 02:09

Gobot