2013-05-09 54 views
0

我有以下代碼,只要它的屬性「文檔」(類型爲UIManagedDocument)被設置,我的View Controller就會使用它。保存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]; 
} 

回答

0

20秒聽起來有點極端,除非你正在處理一個龐大的數據庫。你有沒有試過時間分析儀器中的保存過程?

即使延遲完全在框架代碼中,有時通過深入探索時間配置文件,您可以得到一些它正在做的事情的一些暗示,這需要花費很長時間。例如,它的唯一切線相關,但通過這樣做,我發現保存在iCloud上同步的UIManagedDocuments在數據模型中由於存在反向關係而完全失敗。