I'm creating a core data favorite feature. I have a favorite attribute with type boolean in recipe table. There are 2 views.
First view is for user to select favorite.
Second view display a list of favorites that is selected.
The feature works fine when i go to the first view. However, the app crashed after i go to the second view and go back to first view again. I get the following error:
My code for the method in first view that toggle the favorite attribute to 1 or 0:
Is it a problem that i did not update the database? I have searched around and found the following method, where should i include this into the code?
Can anyone help please?
Thanks!
First view is for user to select favorite.
Second view display a list of favorites that is selected.
The feature works fine when i go to the first view. However, the app crashed after i go to the second view and go back to first view again. I get the following error:
Code:
2012-01-09 18:44:09.455 coredata[1354:12503] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. Can't do regex matching on object 0. with userInfo (null)
2012-01-09 18:44:09.457 coredata[1354:12503] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Can't do regex matching on object 0.'
My code for the method in first view that toggle the favorite attribute to 1 or 0:
Code:
- (IBAction)favoriteButtonPressed:(id)sender {
if (recipe.favorite == 0) {
[favoriteButton setSelected:YES];
favoriteButtonSelected = 1;
[favoriteButtonImage setImage:[UIImage imageNamed:@"selected.png"] forState:UIControlStateNormal];
self.recipe.favorite = [NSNumber numberWithInteger:1];
}else {
[favoriteButton setSelected:NO];
favoriteButtonSelected = 0;
[favoriteButtonImage setImage:[UIImage imageNamed:@"unselected.png"] forState:UIControlStateNormal];
self.recipe.favorite = [NSNumber numberWithInteger:0];
}
NSManagedObjectContext *context = recipe.managedObjectContext;
NSError *error;
if (![context save:&error]) {
NSLog(@"Tried to save favorite Unresolved error %@, %@", error, [error userInfo]);abort();
}
}
Is it a problem that i did not update the database? I have searched around and found the following method, where should i include this into the code?
Code:
[context refreshObject:recipe mergeChanges:NO];
Can anyone help please?