核心数据错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法在对象 0 上进行正则表达式匹配”
我正在创建一个核心数据最喜欢的功能。我最喜欢的属性是配方表中的布尔类型。有 2 个视图。
第一个视图供用户选择最喜欢的。 第二个视图显示所选收藏夹的列表。
当我进入第一个视图时,该功能运行良好。但是,在我进入第二个视图并再次返回第一个视图后,应用程序崩溃了。我收到以下错误:
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.'
我在第一个视图中将最喜欢的属性切换为 1 或 0 的方法的代码:
- (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();
}
}
更新: 是不是我没有更新数据库的问题?我搜索了一下并发现了以下方法,我应该将其包含在代码中的哪里?
[context refreshObject:recipe mergeChanges:NO];
有人可以帮忙吗?谢谢!
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:
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:
- (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();
}
}
Updated:
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?
[context refreshObject:recipe mergeChanges:NO];
Can anyone help please? Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试将
[NSFetchedResultsController deleteCacheWithName:@"root"];
在
它解决了我的问题。希望有帮助
Try to put
[NSFetchedResultsController deleteCacheWithName:@"root"];
in the
It solved me the problem. Hope it helps