基于NSFetchedResultsController的表视图总是在SECOND实体插入时失败 [英] NSFetchedResultsController based Table View always fails on SECOND insert of entity

查看:108
本文介绍了基于NSFetchedResultsController的表视图总是在SECOND实体插入时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用NSFetchedResultsController来管理在具有一个部分的表视图中显示提取的托管对象。表开始为空,用户可以使用UI向其中添加新实体。按照原样,程序总是在添加第一个实体时工作,并且在添加第二个实体时总是崩溃。有时在崩溃和其他时候有错误显示不同类型的错误(下面包括一些)。通过日志语句和跟踪我看到程序崩溃就在NSFetchResultsController的委托的controllerWillChangeContent(它调用[self.tableView beginUpdates];)方法退出之后,但在我的代码中的任何其他方法被调用之前。
这里是我的代码的一些相关的部分。配置NSFetchedResultsController:

I am using an NSFetchedResultsController to manage displaying fetched managed objects in a table view that has one section. The table starts out empty and the user can add new entities to it using the UI. As it stands, the program always works when adding the first entity, and always crashes when adding a second. There is sometimes no error presented upon the crash and other times there are errors of differing types (some included below). Through log statements and tracing I see that the program crashes just after the NSFetchResultsController's delegate's controllerWillChangeContent (which calls [self.tableView beginUpdates];) method exits, but before any other method in my code is called. Here are some of the relavant parts of my code. Configuring the NSFetchedResultsController:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
[fetchRequest setEntity:[NSEntityDescription entityForName:@"Beer"
                                    inManagedObjectContext:self.managedObjectContext]];

// Configure request's entity and predicate
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES];
NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptor release];
[sortDescriptors release];

NSString *expression = [NSString stringWithFormat:@"brewery.name LIKE \"%@\"", self.brewery.name];
NSPredicate *predicate = [NSPredicate predicateWithFormat:expression];
[fetchRequest setPredicate:predicate];
self.resultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
                                                             managedObjectContext:self.managedObjectContext
                                                               sectionNameKeyPath:nil
                                                                        cacheName:nil];
self.resultsController.delegate = self;
[fetchRequest release];

NSError *error = nil;
BOOL success = [resultsController performFetch:&error];
if (!success) {
    NSLog(@"Error fetching request %@", [error localizedDescription]);
}

添加新实体:

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Beer" inManagedObjectContext:self.managedObjectContext];
Beer *beer = [[Beer alloc] initWithEntity:entity insertIntoManagedObjectContext:self.managedObjectContext];
beer.name = beerName;
beer.brewery = self.brewery;

我已经看到了文档中关于显示表格的问题的一些警告,我已经使用苹果解决方法,没有效果。

I have seen the warnings in the docs about problems displaying tables with one section, and I have used Apple's workaround for that to no avail. Those methods aren't getting called prior to the crash anyway.

我收到的一些错误:

Serious application error.  Exception was caught during Core Data change processing: *** -[NSCFString compareObject:toObject:]: unrecognized selector sent to instance 0x4e808c0 with userInfo (null)
Serious application error.  Exception was caught during Core Data change processing: *** -[CALayer compareObject:toObject:]: unrecognized selector sent to instance 0x4e53b80 with userInfo (null)
Serious application error.  Exception was caught during Core Data change processing: *** -[UITextTapRecognizer controllerWillChangeContent:]: unrecognized selector sent to instance 0x4ca5d70 with userInfo (null)
Serious application error.  Exception was caught during Core Data change processing: *** -[CALayer controllerWillChangeContent:]: unrecognized selector sent to instance 0x4e271a0 with userInfo (null)
Serious application error.  Exception was caught during Core Data change processing: *** -[NSCFNumber countByEnumeratingWithState:objects:count:]: unrecognized selector sent to instance 0x4c96ee0 with userInfo (null)

正如您所看到的,即使没有对代码进行任何更改,错误(在提交时也是如此)。

As you can see, the errors (when one was presented) are not consistent, even when no change to the code was made.

任何人都能知道我做错了什么?

Can anyone figure out what I'm doing wrong?

推荐答案

发布问题。特别是因为更改您的委托只是重新加载数据使问题消失。我个人会投入时间来解决这个问题,而不是围绕它编码,因为你肯定有一个代码问题,可能会出现在稍后的其他地方。

Your inconsistent errors point to a possible over-release issue. Especially since changing your delegates to just reload the data makes the issue go away. Personally I would invest the time to solve the issue rather than coding around it as you definitely having a code issue there that will probably show up later somewhere else.

以下内容:


  1. 开启NSZombie

  2. objc_exception_throw

  3. 在调试器中运行

查看内存地址并查看正在访问的内容和位置。这将隔离问题,告诉你发生了什么。如果这没有揭示问题,也会有帮助,发布您的 NSFetchedResultsController 委托方法,所以我可以看到他们是否有什么奇怪。

When the exception occurs, look at the memory address and see what is being accessed and where. This will isolate the issue and tell you what is going on. It would also be helpful, if that does not reveal the issue, to post your NSFetchedResultsController delegate methods so I can see if there is anything strange in them.

最好使用附加信息更新原始问题。

Best to update the original question with the additional information.

这篇关于基于NSFetchedResultsController的表视图总是在SECOND实体插入时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆