0
我已經設法理解如何將UIManagedDocument合併到一個簡單的測試應用程序中,並且它按預期工作!但是,現在我正在爲這個基本應用程序添加支持,因此如果用戶不想使用iCloud,它就可以工作。當無形容器容器(iCloud)不存在時存儲UIManagedDocuments
所以當URLForUbiquityContainerIdentifier:
方法返回「零」,我使用建議的方法
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
return [NSURL fileURLWithPath:documentsDirectoryPath];
然而,當我嘗試保存UIManagedDocument
到本地URL(如返回本地文件目錄的網址: file://localhost/var/mobile/Applications/some-long-identifier/Documents/d.dox
)我得到以下錯誤:
*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'This NSPersistentStoreCoordinator has no persistent stores. It cannot perform a save operation.'
使用這種方法保存:
if (![[NSFileManager defaultManager] fileExistsAtPath:self.managedDocument.fileURL.path]) {
[self.documentDatabase saveToURL:self.managedDocument.fileURL
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success) {
if (success) {
//
// Add default database stuff here.
//
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self.documentDatabase.managedObjectContext performBlock:^{
[Note newNoteInContext:self.managedDocument.managedObjectContext];
}];
});
} else {
NSLog(@"Error saving %@", self.managedDocument.fileURL.lastPathComponent);
}
}];
}