发生了什么:
每当我创建一个新的 Core Data 对象时,我的应用程序都会挂起大约 3 秒。该对象不是大型或复杂的对象。
-
详细信息:
按下按钮,创建一个新对象,设置属性,然后使用以下方法保存:
[[SharedCoreDataBackend managedObjectContext] save:&error];
这导致 NSFetchedResultsController 被更新如下。我已将减速范围缩小到以下代码行:
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView beginUpdates];
[self.tableView deleteRowsAtIndexPaths:self.deletedRowIndexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.tableView insertRowsAtIndexPaths:self.insertedRowIndexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.tableView reloadRowsAtIndexPaths:self.updatedRowIndexPaths withRowAnimation:UITableViewRowAnimationNone];
[self.tableView endUpdates];
}
-
所以...
知道问题出在哪里吗?我可以发布更多有助于解决此问题的测试或代码吗?
最佳答案
尝试使用这样的委托(delegate)方法...
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
switch(type) {
case NSFetchedResultsChangeInsert:
{
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;
case NSFetchedResultsChangeDelete:
{
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;
case NSFetchedResultsChangeUpdate:
{
[self configureCell:tableView cell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
}
break;
case NSFetchedResultsChangeMove:
{
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
}
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
关于ios - NSFetchedResultsController 非常慢,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21057992/