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];
}