NSFetchedResultsController试图插入nil对象? [英] NSFetchedResultsController attempting to insert nil object?

查看:113
本文介绍了NSFetchedResultsController试图插入nil对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

很长一段时间,我遇到一个奇怪的错误,我的大部分视图变为黑色(只有少数单元格保留 http://cl.ly/LFlS ),在其他视图(连接到同一个MOC)中会出现一些其他视觉错误: http://cl.ly/LH3c (注意重复的栏目标题)。我一直认为这是一个与CoreData的错误,但我从来没有重现它,直到今天,而它被挂接到调试器。这是我发生之前的异常:

For a long time I've been getting a strange bug where most of my table view becomes black (only a few cells remain http://cl.ly/LFlS) and in other views (connected to the same MOC) some other visual glitches appear: http://cl.ly/LH3c (notice the duplicate section headers). I always figured it was a bug with CoreData but I never got to reproduce it until today while it was hooked to the debugger. Here's the exception I got right before it happened:


CoreData:错误:严重的应用程序错误。在调用
-controllerDidChangeContent:时,从NSFetchedResultsController的委托捕获到
异常。 * - [__ NSArrayM insertObject:atIndex:]:object不能为nil with userInfo(null)

CoreData: error: Serious application error. An exception was caught from the delegate of NSFetchedResultsController during a call to -controllerDidChangeContent:. * -[__NSArrayM insertObject:atIndex:]: object cannot be nil with userInfo (null)

停止在我的 controllerDidChangeContent:方法中的 [tableView endUpdates] 然后,如果我单击继续,应用程序不会崩溃,但用户交互变得非常缓慢。我看了所有的地方,因为什么可能是该异常的原因,找不到任何东西。任何线索?

It stopped on the [tableView endUpdates] line in my controllerDidChangeContent: method. Then if I click continue, the app doesn't crash, but user interaction becomes extremely sluggish. I looked all over the place as of what might be the cause of that exception and couldn't find anything. Any clue?

我的NSFetchedResultsController更改处理看起来很像苹果的样板。我的NSFRC的init看起来像这样:

My NSFetchedResultsController change handling looks pretty much like Apple's boilerplate. The init of my NSFRC looks like this:

NSFetchRequest *request = [[NSFetchRequest alloc] init];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Artist" inManagedObjectContext:[SWDataManager sharedManager].mainObjectContext];
[request setEntity:entity];

[request setFetchBatchSize:100];

NSSortDescriptor *sortByName = [[NSSortDescriptor alloc] initWithKey:@"sortName" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)];
[request setSortDescriptors:@[sortByName]];

fetchedResultsController = [[NSFetchedResultsController alloc]
                            initWithFetchRequest:request
                            managedObjectContext:mainObjectContext
                            sectionNameKeyPath:@"firstLetter"
                            cacheName:nil];

fetchedResultsController.delegate = self;
[self refreshDataSource]; // set fetch request predicate and call performFetch on NSFRC
return fetchedResultsController;

编辑:我可以补充说,这绝对是发生在一堆对象从我的MOC删除,

I can add that this definitely happened after that a bunch of objects got deleted from my MOC and therefore from the table view.

根据请求,我的 controllerDidChangeContent:代码:

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.tableView endUpdates];

    // this updates the section index and footer cell and other kind of stuff
    [self performSelector:@selector(layoutSpecialCells) withObject:nil afterDelay:0.3];
}


推荐答案

与苹果,这是我得到的答案的要点:

I ended up requesting a TSI with Apple and here's the gist of the answer I got:


为了保护数据存储的完整性,Core Data捕获了一些
在其操作期间发生的异常。有时这意味着
,如果Core Data通过委托方法调用你的代码,Core Data
可能会捕获你的代码抛出的异常。

To protect the integrity of the datastore, Core Data catches some exceptions that happen during it's operations. Sometimes this means that if Core Data calls your code through a delegate method, Core Data may end up catching exceptions your code threw.

这种情况下,Core Data通过尝试使用-insertObject:atIndex将 nil 插入到NSMutableArray中,从
-controllerDidChangeContent: / p>

In this case, Core Data caught an exception through from your -controllerDidChangeContent: method, caused by trying to use -insertObject:atIndex to put nil into an NSMutableArray.

基本上,我的代码抛出了异常,而不一定是Core Data框架。我仍然找不到异常的根本原因,所以建议:

So basically that exception is thrown by my code, and not necessarily by the Core Data framework. I still couldn't find the root cause of the exception, so the advice:


多线程错误是最常见的原因神秘核心数据问题。

Multi-threading errors are the most common cause of mysterious Core Data issues.

进行了很长时间,我意识到一些我的核心数据代码没有封装在 performBlock:调用。一旦我得到固定,我从来没有看到问题再次发生。

went a long way and I realized some of my Core Data code wasn't encapsulated inside performBlock: calls. Once I got that fixed, I never saw the issue happen again.

这篇关于NSFetchedResultsController试图插入nil对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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