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!