我已经设法了解如何将 UIManagedDocument 合并到一个简单的测试应用程序中,并且它可以按预期工作!但是,现在我正在添加对这个基本应用程序的支持,因此如果用户不想使用 iCloud,它也可以工作。
因此,当该URLForUbiquityContainerIdentifier:
方法返回“nil”时,我使用建议的方法返回本地文档目录的 URL
NSString *documentsDirectoryPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
return [NSURL fileURLWithPath:documentsDirectoryPath];
但是,当我尝试将 a 保存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);
}
}];
}