1

我开始了解UIManagedDocument它的能力。据我了解,它是一种存储在文档目录中某个位置的数据库形式。

无论如何,我试图将它用作数据库,通过将数据存储在其中NSManagedObjectContext但是,在创建UIManagedDocument数据并将数据保存到其上下文几秒钟后(在我猜测超时之后),应用程序崩溃并告诉我没有持久的存储协调器。

Apple 文档上似乎没有太多关于如何在创建托管文档后创建持久存储协调器的指导。

我该怎么做?

谢谢!

编辑

我正在使用的代码:

NSURL *url = [[self iCloudDocumentsURL] URLByAppendingPathComponent:nameOfDocument];

NSMutableArray *books = [self.books mutableCopy];
[books addObject:url];

[self setBooks:books];

UIManagedDocument *document = [[UIManagedDocument alloc] initWithFileURL:url];

NSDictionary *options = @{ NSMigratePersistentStoresAutomaticallyOption : @YES,
                           NSInferMappingModelAutomaticallyOption : @YES,
                           NSPersistentStoreUbiquitousContentNameKey : document.fileURL.lastPathComponent,
                           NSPersistentStoreUbiquitousContentURLKey : [self iCloudCoreDataLogFilesURL] };
[document setPersistentStoreOptions:options];

if (document.documentState == UIDocumentStateClosed) {
    [document openWithCompletionHandler:^(BOOL success) {
        Book *book = [Book newBookWithTitle:bookTitle
                     inManagedObjectContext:document.managedObjectContext];

        [document updateChangeCount:UIDocumentChangeDone];
    }];
}

更新

在后台线程上执行此代码后。打开文档后,我的应用程序仍会在完成处理程序上成功记录“否”。但是,我的应用程序没有崩溃,而是得到以下日志:

2013-04-23 00:01:08.381 Notable[193:4b0b] -[_PFUbiquityRecordsImporter rollResponseOperation:encounteredAnError:whileTryingToAdoptBaseline:](1545): CoreData: Ubiquity:  <PFUbiquityBaselineRollResponseOperation: 0x1fa561d0> localPeerID: mobile.2DDB96C5-A317-5BE5-8F29-9F5E6681A27A, storeName: A-B893A0AB-B764-42F1-9402-38790DCEF96B, modelVersionHash: NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=
    ubiquityRootLocation: <PFUbiquityLocation: 0x1e59b790>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs

Encountered an error while trying to respond to the roll of baseline: <PFUbiquityBaseline: 0x1e54deb0>(0)
    permanentLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip
    safeLocation: <PFUbiquityLocation: 0x1f8eb1b0>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/mobile.2DDB96C5-A317-5BE5-8F29-9F5E6681A27A
    currentLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip

    storeName: A-B893A0AB-B764-42F1-9402-38790DCEF96B
    modelVersionHash: NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=
    baselineArchiveLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip

Error: Error Domain=NSCocoaErrorDomain Code=134310 "The operation couldn’t be completed. (Cocoa error 134310.)" UserInfo=0x1f8d2ae0 {baseline=<PFUbiquityBaseline: 0x1e54deb0>(0)
    permanentLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip
    safeLocation: <PFUbiquityLocation: 0x1f8eb1b0>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/mobile.2DDB96C5-A317-5BE5-8F29-9F5E6681A27A
    currentLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip

    storeName: A-B893A0AB-B764-42F1-9402-38790DCEF96B
    modelVersionHash: NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=
    baselineArchiveLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip
, localStoreKV=<PFUbiquityKnowledgeVector: 0x1f8220c0> ()}
userInfo: {
    baseline = "<PFUbiquityBaseline: 0x1e54deb0>(0)\n\tpermanentLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip\n\tsafeLocation: <PFUbiquityLocation: 0x1f8eb1b0>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/mobile.2DDB96C5-A317-5BE5-8F29-9F5E6681A27A\n\tcurrentLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip\n\n\tstoreName: A-B893A0AB-B764-42F1-9402-38790DCEF96B\n\tmodelVersionHash: NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=\n\tbaselineArchiveLocation: <PFUbiquityLocation: 0x1f8e7120>: /private/var/mobile/Library/Mobile Documents/my~app~identifier/Logs/.baseline/A-B893A0AB-B764-42F1-9402-38790DCEF96B/NK3BysbustJxxyi2O8bX8eof15YimkUnegxqX6HDkas=/baseline.zip\n";
    localStoreKV = "<PFUbiquityKnowledgeVector: 0x1f8220c0> ()";
}

此方法还创建一个“DocumentMetaData.plist”(根据我在评论中提到的 CS193P 视频中提到的这些文件,我认为这是必要的)而不是一个名为“persistentStore”的文件。

4

3 回答 3

1

不确定您是否曾经修复过此 Adam,但您的 persistentStore 不应该在云端。始终在本地存储中创建 uiManagedDocument,并为 addPersistentStore 设置 iCloud 选项。CoreData 将提供一个即时存储供您的应用程序在构建真实版本时使用,并从 iCloud 获取任何数据(如果有)。完成后,它会将您切换到本地的 iCloud 连接商店。只有交易日志会发布到 iCloud,但这是在幕后为您完成的。

于 2013-10-07T11:30:57.170 回答
1

使用 UIManagedDocument 时,您不应该自己创建它——这会发生在您身上。但是在实践中它并不那么可靠。在内部,UIManagedDocument 设置持久存储和持久存储协调器。但这不会发生,直到底层 iCloud 代码完成其动作、与服务器通信、下载任何新数据等。与此同时,您无法保存更改,因为在此过程完成之前,无处保存这些变化。

此外,有时 iCloud 会无缘无故地无法协调行动,这在您的控制之下。

您可能需要检查success打开完成处理程序中的值。如果是NO,你有点卡住了。当 iCloud 崩溃时,没有恢复路径,除非有时如果你很幸运,你可以再试几次,直到它起作用。请提交错误,我们需要 Apple 来解决这个问题。

于 2013-04-22T22:19:34.853 回答
0

UIDocument Class ReferenceCore Data Programming Guide中有示例代码。您还可以通过使用 Master-Detail 模板创建基于 Core Data 的项目来查看 Core Data 堆栈设置的示例。

如果这是您第一次接触 Core Data,我建议您阅读有关该主题的书籍,要么是Core Data for iOS,作者 Tim Isted 和 Tom Harrington,要么是Core Data (2nd edition): Data Storage and Management for iOS 、OS X 和 iCloud,作者:Marcus Zarra。

我看到一些看起来很奇怪的事情:

  • 是否[self iCloudDocumentsURL]指向本地文件系统?

  • 为什么将 URL 插入到书籍数组中?

  • success在假设打开成功之前测试块中的变量。

即使在 iOS 6.x 中,iCloud Core Data 也没有准备好迎接黄金时间。这当然不是新的 iOS 开发者想要解决的问题。看看 Black Pixel 非常聪明的人对他们自己的产品之一的评价:http: //blackpixel.com/blog/2013/03/the-return-of-netnewswire.html

于 2013-04-22T21:16:00.260 回答