我最近从一个离岸团队继承了一个代码库,我没有UIManagedDocument的经验。在某些情况下,首次安装应用程序时,初始化过程会完全锁定应用程序。它似乎与iCloud直接相关,因为当网络关闭时,应用程序立即加载。
我尝试在gcd块中执行此代码的各个部分,但是有一些进程仍然会转移到主线程并阻止所有输入。
无论如何要保存或异步打开?
NSURL *_url = [[self applicationDocumentDirectory] URLByAppendingPathComponent:MyDatabaseName];
self.document = [[MyCustomManagedDocument alloc] initWithFileURL:_url];
self.document.delegate = self;
NSMutableDictionary *options = [NSMutableDictionary dictionaryWithObjectsAndKeys:
[NSNumber numberWithBool:YES],NSMigratePersistentStoresAutomaticallyOption,
[NSNumber numberWithBool:YES], NSInferMappingModelAutomaticallyOption,nil];
NSURL *iCloudURL = [self iCloudDocumentsURL];
[options setObject:PrivateName forKey:NSPersistentStoreUbiquitousContentNameKey];
[options setObject:iCloudURL forKey:NSPersistentStoreUbiquitousContentURLKey];
self.document.persistentStoreOptions = options;
if ([[NSFileManager defaultManager] fileExistsAtPath:[_url path]]) {
[self.document openWithCompletionHandler:^(BOOL success){
if (success) {
[self documentIsReady];
}
}];
}
else {
// This can take 10 seconds sometimes.
[self.document saveToURL:_url forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
if (success) {
[self documentIsReady];
}
}];
}