UIManagedDocument + iCloud“Big Picture”? [英] UIManagedDocument + iCloud "Big Picture"?

查看:321
本文介绍了UIManagedDocument + iCloud“Big Picture”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发我的第一个iCloud应用程序。我通过苹果文档和斯坦福视频,但我仍然很难理解iCloud的大图片。



我的目标是创建一个库样式应用程序(这是一个应用程序的Apples术语,包含具有单个持久存储协调器和单个持久存储的单个Core数据堆栈),如员工示例



按照斯坦福视频,我使用UIMangedDocument来设置所有Core Data东西并启用iCloud功能。 UIMangedDocument包含数据库,并且存储在云中。



这是我第一次遇到的问题:存储在云真的是吗?



在我开始使用iCloud之前,我认为将文档存储在云端意味着文档在云。我认为在常规沙箱中有本地版本的文档,在云中有文档的副本。当本地文档更改时,这些更改也会传输到云版本。正如我明白现在这是不正确的(至少不完全正确)。我是对的吗?



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



我的第一个假设(本地文件+云中的副本) 。实际上,存在在云中的任何文件的本地版本和云版本。这是因为我可以访问iCloud URL并在此位置存储文件,即使设备当前没有Internet连接。但这只是在iCloud框架的魔力之后,没有什么我不得不打扰。从我的角度来看,云是简单的一个特殊的文件夹,这个文件夹中的任何文件或目录都存储在云中。如果在设备设置中禁用iCloud,iCloud URL将失效。 是吗?



我努力的第二件事是iCloud如何将更改同步到文档。假设设备A上的TheApp创建一个UIManagedDocument并存储在云中。设备A脱机后。同时,文档由设备B上的App访问,并且添加一些数据(例如,插入一些新员工或部门)。当设备B再次联机时,它将接收 NSPersistentStoreDidImportUbiquitousContentChangesNotification 并且可以合并所有在文档的managedObjectContext上调用 mergeChangesFromContextDidSaveNotification:以复制这些更改。



我无法找出合并更改是否真的必要。如上所述,从我的角度来看,只有一个文档。如果这是真的,我很高兴通知有关更改,但没有必要将这些更改复制到文档的本地版本,因为没有这样的事情,作为本地版本。 这带来了什么是mergeChangesFromContextDidSaveNotification:



另一种情况将是与以前相同的情况,在设备B上更改...),但现在设备A不只是脱机,但是在设备B上进行更改时,App完全关闭。在这种情况下,设备A上的App将不得不重新创建/重新打开文档之后再次被笑。在这种情况下,应该会发生什么?



我做了一些测试,但结果并不总是一样。在某些情况下,文档以其旧版本开始,而不是接收到带有更改的通知。在其他情况下,文档直接在其新版本中启动,所有更改都在板上。对于我的应用程序的第一种情况(从旧版本开始,接收更新通知)会更好,但我不能强制一个或其他行为。这是它应该工作吗?



非常感谢!

解决方案

一般来说,我可以推荐阅读 iCloud设计指南 - 特别是在iCloud中设计文档



至于您的问题:




  • 存储在云中是什么意思?



    如果您使用的是UIManagedDocument您的应用程序将在添加/编辑/删除数据时上传更改日志。然而,您的设备有一种iCloud缓存,其中存储iCloud数据和访问(您可以访问该文件夹,当您离线或甚至当您的应用程序被删除和重新安装)。如果您 NSLog 您的iCloud文件夹中的文档的URL(您应该使用 NSMetadataQuery 您的设备上的iCloud文件是



    / private / var / mobile / Library / Mobile Documents /< Developer identifier> /< ;应用程序标识符> / ...



    为此,您需要考虑一系列访问您的UI在第一次启动应用程序或每次启动应用程序时,iCloud上的文档(例如,iCloud可用,是否具有网络连接...)。



    按照视频的方式,您不会在应用程序沙箱和iCloud中有单独的文档(当您将文档保存到iCloud网址以及应用沙箱中的本地网址时,您可以实现这一点)。

    设备正在保存更改日志,其中包括对文档的添加/编辑/删除。这些更改日志将上传到iCloud并由连接到同一iCloud帐户的其他设备下载。每个设备都可以使用更改日志重建数据模型的当前状态。这使得iCloud极其高效(每次完全上传需要更长的时间)。关于这些变化的同步过程,我也可以参考iCloud设计指南。总之,每次当它回到在线模式时,你都必须考虑你的设备同步本地iCloud文件夹。



I am working on my first "iCloud App". I worked through the Apple docs and the Stanford videos but I am still struggling to understand the "Big picture" of iCloud.

My goal is to create a "Library style" app (which is Apples term for an app with "a single Core Data stack with a single persistent store coordinator and a single persistent store") like the Employees example.

Following the Stanford videos I am using a UIMangedDocument to setup all Core Data stuff and enable the iCloud capabilities. The UIMangedDocument contains the Database and is "stored in the cloud".

This is already the first thing I struggle with: What does "stored in the cloud" really mean?

Before I started to work with iCloud I thought that to "store a document in the cloud" would mean to "store a copy of the document in the cloud". I thought there would be local version of my document within the regular sandbox and a copy of the document in the cloud. When ever the local document is changed these changes are also transfered to the cloud version. As I understand now this is not correct (at least not completely correct). Am I right?

The first thing I have to do to use iCloud is to call URLForUbiquityContainerIdentifier:. This will return the "iCloud URL" which means the URL of the folder in the cloud. All files which are stored under this URL will be "stored in the cloud", right?

My first assumption (local file + copy in the cloud) is not completely wrong. In fact there is a "local version" and a "cloud version" of any file that is stored in the cloud. This is because I can access the iCloud URL and store files at this location even if the device has currently no Internet connection. But this is only behind the magic of the iCloud framework and nothing I have to bother with. From my point of view the cloud is simple a special folder and any file or directory in this folder is stored in the cloud. The iCloud URL will ony be NIL if iCloud is deactivated in device setting. Is that right?

The second thing I struggle with is how iCloud will sync changes to a Document. Assume "TheApp" on device A creates a UIManagedDocument and stores in in the Cloud. After that device A goes offline. Meanwhile the document is accessed by TheApp on device B and some data is added (e.g. some new employees or departments are inserted). When device B goes online again it will receive the NSPersistentStoreDidImportUbiquitousContentChangesNotification and can merge all call mergeChangesFromContextDidSaveNotification: on the managedObjectContext of the document to copy these changes.

I was not able to find out if merging the changes is really necessary. As described above about from my point of view there is only one Document. If that is true, it is great that I am notified about changes but it should not be necessary to copy these changes to the "local version" of the document because there is not such thing as a "local version". This brings me to the question what mergeChangesFromContextDidSaveNotification: is good for.

Another scenario would be the same situation as before (document created on device A, changed on device B...) but now device A was not just offline but TheApp was completly shutdown while the changes were made on device B. In that case TheApp on device A will have to re-create/re-open the document after it is lauched again. What should happen in this case?

I did run some tests but the result was not always the same. In some cases the document started with its "old" version and than received the notification with the changes. In other cases the document started directly in its new version with all changes on board. For my app the first case (start with old version, receive update notification) would be better, but I was not able force the one or the other behavior. Is this the was it is supposed to work?

Thank you very much!

解决方案

Generally I can recommend to read the iCloud Design Guide - in particular the section "Designing for Documents in iCloud".

As for your questions:

  • What does "stored in the cloud" really mean?

    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.

这篇关于UIManagedDocument + iCloud“Big Picture”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆