【发布时间】:2012-04-08 13:00:34
【问题描述】:
[编辑] 在更准确地查明问题后,我将对此进行更改以更简洁地解释我的问题。
我正在为我的应用程序处理核心数据,但我很难过。它每次都挂在这个方法中。没有崩溃,没有日志,什么都没有。它只是挂起。
- (void)insertNewObject:(id)sender
{
NSManagedObjectContext *context = [self.fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
Section *newSection = (Section *)newObject;
newSection.title = @"inserted";
NSError *error = nil;
if (![context save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
我发现如果我将 NSLogs 放在这两个委托方法中:
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
他们只是不断被调用无数次。
【问题讨论】:
-
看起来它可能在您的获取结果控制器委托方法(didChangeContent)中发生了什么?插入方法是从哪里调用的?
-
@jrturton 我的代码中唯一调用 insertNewObjectForEntityForName: 的地方是在上面的 insertNewObject: 方法中,并且我已经通过日志确认 insertNewObject: 仅被调用一次。我在 controllerWillChangeContent: 中拥有的只是 [self.tableView beginUpdates];也在controllerDidChangecntent:我只有[self.tableView endUpdates];
-
我遇到了同样的问题(无限循环)。我的 NSManagedOjbect 中有一个 cachedCellHeight 瞬态属性。在我的视图控制器中,我使用 tableView 设置 NSFetchedResultController,并计算 tableView:tableView heightForRowAtIndexPath: 中的 cellHeight 并将其保存在 cachedCellHeight 属性中,这会导致无限循环!如果你有同样的问题,也许你应该再次检查你的代码,看看是否有可能导致无限循环(例如更新 NSFetchedResultsControllerDelegate 协议方法中的某些内容!!!)