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