核心数据错误:由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“无法在对象 0 上进行正则表达式匹配”

发布于 2024-12-26 05:36:58 字数 1780 浏览 2 评论 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱武穆 2025-01-02 05:36:58

尝试将 [NSFetchedResultsController deleteCacheWithName:@"root"];

- (NSFetchedResultsController *)fetchedResultsController{
...} 

它解决了我的问题。希望有帮助

Try to put [NSFetchedResultsController deleteCacheWithName:@"root"];

in the

- (NSFetchedResultsController *)fetchedResultsController{
...} 

It solved me the problem. Hope it helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文