NSFetchedResultsController(和 UITableView)委托方法调用不断增长

NSFetchedResultsController (and UITableView) delegate methods calls growing and growing

设置:带有 FRC 的 UITableView。行是简单的文本内容列表,用户可以拉动以刷新以获取最新信息。

我看到奇怪的行为,其中多次为每一行调用 cellForRow。所以我看到它是 0,0 0,1 0,2 0,3(可见行),但是这 4 行都多次调用了 cellForRow。但第一次查看列表时,它们会被调用一次。第二次,两次,等等。到第 7 次,用户看到内容后,它在幕后继续一遍又一遍地尝试和配置单元,最终崩溃。

因此,如果您转到任何内容列表,它会访问服务器,下载故事,创建 NSMO 并显示。在日志中,我看到 configureCell 为每个可见行调用一次。如果我刷新,我会看到相同的内容。但是,如果我导航到另一个屏幕,然后返回,当我拉动刷新时,我注意到 cellforrow 每行被调用两次。如果我继续这个离开和回来的过程,每次我这样做,都会额外调用一次 cellforrow。记录一些获取的结果控制器委托方法,我在每组 cellforrow 调用之前看到 willchangecontent。谁能帮我确定为什么我的 cellforrow 方法被调用的次数越来越多?

一个想法是我设置 FRC 的方式。我遵循了 CoreDataBooks 之类的代码并将内容移至 viewdidload,但仍然看到问题。

我在 .h 中有一个 属性,在 .m 中有我认为是标准设置的内容:

- (NSFetchedResultsController *)fetchedResultsController
{
//NSLog(@"fetchedresulscontroller");
if (_fetchedResultsController != nil)
{
    return _fetchedResultsController;
}

// initialize fetch request. setup predicate, sort, etc.

NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:@"date" cacheName:nil];

aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;

// perform actual fetch. delegate methods take it from here
NSError *fetchError = nil;
if (![self.fetchedResultsController performFetch:&fetchError])
{
    // Replace this implementation with code to handle the error appropriately.
    // abort() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
    NSLog(@"Unresolved error %@, %@", fetchError, [fetchError userInfo]);
    abort();
}

return _fetchedResultsController;
}

andrewbuilder 走在正确的轨道上。这一切都与 FRC 有关,但诀窍是用于菜单的第三方 SWReveal 库。事实证明,我每次都在创建一个新的 VC(之前的未被释放)并且 FRC 正在查看所有实时取景控制器。因此,每次我从菜单中点击一个选项时,都会添加另一个选项,并为此调用配置调用。

解决办法是在viewwilldisappear中取消FRC委托,在viewwillappear中设置