ios - Core Data 保存时性能不佳 : and [UITableView endUpdates]

标签 ios core-data ios7 restkit

我在使用 Core Data 时遇到了一些性能问题,我希望有人能给我一些关于如何改进它的提示。调用save:时对于我的NSManagedObjectContext ,单个实体的保存时间超过 1 秒。我正在主线程上执行保存,因此在此期间它会锁定 GUI,这是 Not Acceptable 。这是我正在执行保存的代码;此代码在修改 WTSRecord 的“描述”字段后启动:

- (void)saveRecord:(WTSRecord *)record
           success:(void (^)(WTSRecord *record))success
           failure:(void (^)(NSError *error))failure {
    DDLogVerbose(@"saveRecord: %@", record.objectID);

    if (record.recordId != nil) {
        //mark this record as a pending modify if it has a database id
        record.pendingModify = [NSNumber numberWithBool:YES];
    }
    NSError *error;
    NSManagedObjectContext *context = record.managedObjectContext;
    NSTimeInterval startTime = [[NSDate date] timeIntervalSince1970];
    if (![context saveToPersistentStore:&error]) {
        failure(error);
    } else {
        NSLog(@"time to persist record: %f", ([[NSDate date] timeIntervalSince1970] - startTime));

        ...do other stuff here...

    }
}

我打开了 SQL 调试,Core Data 只是更新了一条记录,似乎没有做任何不寻常的事情:
2014-08-13 11:25:32.528 Identify[5395:60b] CoreData: sql: BEGIN EXCLUSIVE
2014-08-13 11:25:32.530 Identify[5395:60b] CoreData: sql: UPDATE ZWTSRECORD SET ZDESC = ?, Z_OPT = ?  WHERE Z_PK = ? AND Z_OPT = ?
2014-08-13 11:25:32.531 Identify[5395:60b] CoreData: details: SQLite bind[0] = "ffffffffffffuuuuiiiiuu"
2014-08-13 11:25:32.532 Identify[5395:60b] CoreData: details: SQLite bind[1] = (int64)48
2014-08-13 11:25:32.533 Identify[5395:60b] CoreData: details: SQLite bind[2] = (int64)306
2014-08-13 11:25:32.534 Identify[5395:60b] CoreData: details: SQLite bind[3] = (int64)47
2014-08-13 11:25:32.535 Identify[5395:60b] CoreData: sql: COMMIT
2014-08-13 11:25:32.538 Identify[5395:60b] time to persist record: 1.376321

这似乎是一个非常简单的更新,真的不应该花那么长时间。大约有 40 个 WTSRecord s 在这个场景中的 sqllite 数据库中。如果我删除了 sqllite 存储并且只修改了一个 WTSRecord ,持续时间不到 1/20 秒。

我看到了this post关于异步储蓄的 cocoa 神经学,但我只想知道在我走这条路之前我是否在做一些根本性的错误。提前致谢!

编辑 1:

附件是 Time Profiler 的屏幕截图。看起来 1.3 秒专用于运行 controllerDidChangeContent:在我的UITableViewController ,即绘制表格 View 单元格。为什么要花那么长时间?

Time Profiler

编辑 2

这是我的NSFetchedResultsControllerDelegate方法。他们并没有真正改变样板 Apple 代码:
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    DDLogVerbose(@"FRC calling beginUpdates in controllerWillChangeContent");
    [self.tableView beginUpdates];
    DDLogVerbose(@"FRC done calling beginUpdates in controllerWillChangeContent");
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
           atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
    switch(type) {
        case NSFetchedResultsChangeInsert:
            DDLogVerbose(@"FRC inserted section");
            [self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            DDLogVerbose(@"FRC deleted section");
            [self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
       atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
      newIndexPath:(NSIndexPath *)newIndexPath
{
    UITableView *tableView = self.tableView;

    switch(type) {
        case NSFetchedResultsChangeInsert:
            DDLogVerbose(@"FRC inserted object");
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeDelete:
            DDLogVerbose(@"FRC deleted object");
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;

        case NSFetchedResultsChangeUpdate:
            DDLogVerbose(@"FRC updated object");
            [self configureCell:[tableView cellForRowAtIndexPath:indexPath] atIndexPath:indexPath];
            break;

        case NSFetchedResultsChangeMove:
            DDLogVerbose(@"FRC moved objects");
            [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
            [tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    DDLogVerbose(@"FRC calling endUpdates in controllerDidChangeContent");
    [self.tableView endUpdates];
    DDLogVerbose(@"FRC done calling endUpdates in controllerDidChangeContent");
}

我添加了一些打印输出,发现绝大多数时间都花在 [self.tableView endUpdates] 上。 .这是我的日志的打印输出:
2014-08-14 10:44:39:663 Identify[5718:60b] Saving to context(<NSManagedObjectContext: 0x145b82c0>)
2014-08-14 10:44:39:666 Identify[5718:60b] FRC calling beginUpdates in controllerWillChangeContent
2014-08-14 10:44:39:666 Identify[5718:60b] FRC done calling beginUpdates in controllerWillChangeContent
2014-08-14 10:44:39:668 Identify[5718:60b] FRC updated object
**2014-08-14 10:44:39:671 Identify[5718:60b] FRC calling endUpdates in controllerDidChangeContent
**2014-08-14 10:44:40:889 Identify[5718:60b] FRC done calling endUpdates in controllerDidChangeContent
2014-08-14 10:44:41:018 Identify[5718:60b] Time to save in context(<NSManagedObjectContext: 0x145b82c0>): 1.355229

最佳答案

现在您已经确定您的问题不在于您的保存,而在于您的绘图代码,因此有一些选项。

  • 挖掘你的手 secret 码,看看是什么花了这么长时间。计算什么?绘制阿尔法?
  • 将您的存档移出主队列。

  • 选项 2 不能解决您的重绘问题,但会减少您的保存时间。您可以通过在主 MOC 和 PSC 之间放置一个私有(private) MOC 来做到这一点。然后保存离开主队列。但是,您的绘图代码仍然会阻塞。

    我会深入每个单元格,看看发生了什么。使用工具打开源代码并找出哪些代码行昂贵,然后探索原因。 Alpha绘图是一个常见问题。图像加载等也可能很受欢迎。

    关于ios - Core Data 保存时性能不佳 : and [UITableView endUpdates],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25291301/

    相关文章:

    ios - MPMoviePlayerController 经常停顿

    ios - iOS中performSelector有什么用

    iphone - Three20 是否提供替代 CoreData 框架的简化安装?

    ios - 有没有办法在呈现另一个ViewController之后将其关闭?

    ios - 如何在 iOS 中将本地(文档目录)html 加载到 webview 中?

    ios - 我是否需要为不同类型的注释创建实现 MKAnnotation 协议(protocol)的类?

    ios - 容器 View 根据子项设置高度

    ios - 应用程序更新后核心数据存储为空 - 但数据模型未更改

    iPhone CoreData 查询

    ios - UISearchBar范围栏在iOS7中不可见