How to update a NSManagedObject

Go To


I have an NSMutableArray with a few NSManagedObjects in it which I fetched from my managedObjectContext. How can I edit the managedObjects and write replace the old object with the new one?

I'm taking the object like this from the array:

NSManagedObject *managedObject = [_dataArray objectAtIndex:indexToTest];
[managedObject setValue:@"sucessfully updated!" forKey:@"hint"];

My problem is, how can I update the managedObject in the managedObjectContext?

Thanks for helping

2012-04-03 21:21
by user1311441


  • Get the NSManagedObject and set value

NSManagedObject *object = [[self fetchedResultsController] objectAtIndexPath:indexPath];
[object setValue: self.urlField.text forKey:@"url"];

  • then get NSManagedObjectContext object and save it

NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSError *error = nil;
if (![context save:&error]) {}

2013-01-18 15:49
by user1990959