UIManagedDocument + iCloud“大图”?

And*_*ord 5 core-data icloud uimanageddocument

我正在开发我的第一个“iCloud 应用程序”。我浏览了 Apple 文档和斯坦福大学视频,但我仍然很难理解 iCloud 的“大局”。

我的目标是创建一个“库风格”应用程序(这是 Apple 的术语,指的是具有“单个核心数据堆栈、单个持久性存储协调器和单个持久性存储”的应用程序),如员工示例

在斯坦福大学视频之后,我使用 UIMangedDocument 来设置所有核心数据内容并启用 iCloud 功能。UIMangedDocument 包含数据库并“存储在云中”。

这已经是我纠结的第一件事:“存储在云端”的真正含义是什么?

在我开始使用 iCloud 之前,我认为“在云中存储文档”意味着“在云中存储文档的副本”。我认为常规沙箱中会有我的文档的本地版本,并且云中会有该文档的副本。当本地文档发生更改时,这些更改也会传输到云版本。据我现在了解,这是不正确的(至少不完全正确)。我对吗?

要使用 iCloud,我要做的第一件事就是调用URLForUbiquityContainerIdentifier:。这将返回“iCloud URL”,这意味着云中文件夹的 URL。存储在该 URL 下的所有文件都将“存储在云端”,对吗?

我的第一个假设(本地文件+云中的副本)并不是完全错误的。事实上,存储在云中的任何文件都有“本地版本”和“云版本”。这是因为即使设备当前没有互联网连接,我也可以访问 iCloud URL 并将文件存储在该位置。但这只是 iCloud 框架的神奇之处,我不必担心。从我的角度来看,云只是一个特殊的文件夹,该文件夹中的任何文件或目录都存储在云中。仅当在设备设置中停用 iCloud 时,iCloud URL 才会为 NIL。是对的吗?

我遇到的第二件事是 iCloud 如何将更改同步到文档。假设设备 A 上的“TheApp”创建了一个 UIManagedDocument 并存储在云端。之后设备A离线。同时设备B上的TheApp访问该文档并添加一些数据(例如插入一些新员工或部门)。当设备 B 再次上线时,它将收到NSPersistentStoreDidImportUbiquitousContentChangesNotification ,并可以合并所有在文档的 ManagedObjectContext 上调用mergeChangesFromContextDidSaveNotification:来复制这些更改。

我无法确定合并更改是否真的有必要。如上所述,从我的角度来看,只有一个文档。如果这是真的,那么很高兴我收到有关更改的通知,但没有必要将这些更改复制到文档的“本地版本”,因为不存在“本地版本”之类的东西。这让我想到了 mergeChangesFromContextDidSaveNotification: 有什么好处的问题。

另一种情况与以前的情况相同(在设备 A 上创建文档,在设备 B 上更改...),但现在设备 A 不仅离线,而且在设备 B 上进行更改时 TheApp 完全关闭。在这种情况下,TheApp设备 A 上的文档再次启动后必须重新创建/重新打开该文档。在这种情况下应该发生什么?

我确实进行了一些测试,但结果并不总是相同。在某些情况下,文档以其“旧”版本开始,然后收到更改通知。在其他情况下,文档直接从新版本开始,并进行了所有更改。对于我的应用程序,第一种情况(从旧版本开始,接收更新通知)会更好,但我无法强制执行这种或另一种行为。这是它应该起作用的吗?

非常感谢!

Fra*_*kZp 1

一般来说,我建议阅读iCloud 设计指南- 特别是“在 iCloud 中设计文档”部分。

至于你的问题:

  • “存储在云端”的真正含义是什么?

    If your are using UIManagedDocument following the Stanford videos, your App will upload change logs when data is added/edited/deleted. However your device has kind of a "iCloud Cache" where it stores the iCloud data and access it from (you have access to that folder when you are offline or even when your app is deleted and reinstalled). If you NSLog the URLs of the documents in your iCloud folder (you should do this with NSMetadataQuery you get the path of the iCloud files locally on your device, which is

    /private/var/mobile/Library/Mobile Documents/<Developer identifier>/<App identifier>/...

    For this reason, you'll need to think about a bunch of things when accessing your UI(Managed)Documents on iCloud on the first time app launch or on each app launch (e.g. is iCloud available, do you have network connection, ...).

    However, following the way of the videos you'll not have separate documents in your app sandbox and in iCloud (you could achieve this of course when saving your document for creation to the iCloud URL as well as to a local URL in the app sandbox).

  • The need of merging changes

    As described above, your devices are saving change logs which include the adds/edits/deletes to your document. These change logs are uploaded to iCloud and downloaded by the other devices connected to the same iCloud account. Each device can rebuild the current state of your data model with the change logs. This makes iCloud extremly efficient (a complete upload on each time would take much longer). Concerning the sync process of these changes I can also refer to the iCloud Design Guide. In short, you'll have to think about your device syncing the local iCloud folder each time when it comes back to online mode.