【问题标题】:Saving a UIManagedDocument - speed improvements保存 UIManagedDocument - 速度改进
【发布时间】:2013-05-04 12:36:36
【问题描述】:

每当设置属性“文档”(UIManagedDocument 类型)时,我都会在视图控制器上使用以下代码。

我不确定其他人是否这样做,但我发现 Core Data 中的并发概念非常令人困惑,文档中有一些解释它,但仍然很难掌握。出于这个原因,我想知道人们是否对如何加快我用来保存新设置的UIDocument 的代码(如果它不存在)有任何想法。如果其他人想使用此代码,它确实有效。

我的主要目标是尽量缩短文档保存和加载所需的时间。目前完成此操作大约需要 20 秒,这太长了!

[[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:YES];

if (![[NSFileManager defaultManager] fileExistsAtPath:self.documentDatabase.fileURL.path]) {
    [self.documentDatabase saveToURL:self.documentDatabase.fileURL
                    forSaveOperation:UIDocumentSaveForCreating
                   completionHandler:^(BOOL success) {
                       [[UIApplication sharedApplication] setNetworkActivityIndicatorVisible:NO];

                       if (success) {
                           NSLog(@"Saved %@", self.documentDatabase.localizedName);

                           dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                               [self.documentDatabase.managedObjectContext performBlockAndWait:^{
                                   dispatch_async(dispatch_get_main_queue(), ^{
                                       [Book newBookWithTitle:self.documentDatabase.fileURL.lastPathComponent.stringByDeletingPathExtension inManagedObjectContext:self.documentDatabase.managedObjectContext];

                                       [self saveDocumentWithCompletionHandler:^(bool success) {
                                           if (success) {
                                               [self setIsDocumentHidden:NO];
                                           }
                                       }];

                                       NSLog(@"Added default note to %@", self.documentDatabase.fileURL.lastPathComponent);
                                   });
                               }];
                           });
                       } else {
                           NSLog(@"Error saving %@", self.documentDatabase.fileURL.lastPathComponent);
                       }
                   }];
} else {
    [self openDocumentWithCompletionHandler:nil];
}

【问题讨论】:

    标签: core-data icloud uimanageddocument


    【解决方案1】:

    20 秒听起来有点极端,除非您处理的是庞大的数据库。您是否尝试过在 Instruments 中对保存过程进行时间分析?

    即使延迟完全在框架代码中,有时通过深入研究时间配置文件,您也可以得到一些提示,了解它在做什么需要这么长时间。例如,它只是切线相关的,但通过这样做,我发现保存通过 iCloud 同步的 UIManagedDocuments 会因数据模型中存在反向关系而完全破坏性能。

    【讨论】:

      猜你喜欢
      • 2012-01-12
      • 1970-01-01
      • 1970-01-01
      • 2020-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多