Search code examples
core-dataios7iclouduimanageddocument

Proper use of iCloud fallback stores


I am working on a lightweight wrapper for UIManagedDocument which manages the existence of multiple documents in a person's iCloud account. See: APManagedDocument

I am relying heavily on the use of fallback stores in iOS 7 and caring as little as possible about the current state of iCloud. Thus letting core data do what it does best in regards the fallback stores based on my understanding from the WWDC 2013 Video - Session 207 What’s New in Core Data and iCloud

First a quick overview of how my manager works: I create all my UIManagedDocuments in the local sandbox and set the appropriate persistent store options to enable iCloud sync. I never move the UIManagedDocument package from the sandbox.

  • When I want to know what documents exist in the cloud I perform a metadata query.
  • When I want to open one of those documents I check to see if it exists in the local sandbox first and if not create it in the local sandbox. (This requires that the app needs to wait for the notification corresponding to the Using local storage: 0 message.)
  • With this set up I never need to know if iCloud is enabled or logged in. I just work locally and let core data do its thing with iCloud.

So far everything is working great but I ran into a little pickle with the scenario where the user creates a new document prior to logging into iCloud and I am presented with the following issues:

  • I cannot perform a metadata query because there is no iCloud to query.
  • Because of 1 I have to fall back to doing an intelligent local scan looking for packages that have 'local/store/persistentStore' in their path and listing those as valid documents.
  • Later when the user logs in it was my understanding that core data would move my local store data to the cloud but I am not seeing that. What I am seeing instead is that a new persistent store is created for the iCloud account and no data.

My big question is what is the proper approach when it comes to the local fallback store? Where are my assumptions wrong?

This is one of the final pieces I need to ship my iOS 7 update. Any and all feedback would be greatly appreciated, and will be reflected in my github project so others can learn from my mistakes.

I have a duplicate of this question in the Apple Developer Forums. I will update this thread with any findings I get from there. I think this question is important and remains unresolved with the release of iOS 7. Fallback stores are a huge advancement in iCloud technology but the local storage part is still a little undefined.


Solution

  • I have worked around this for now since I cannot seem to get information as to how fallback stores are supposed to work in this scenario.

    Basically what I do now is if the user is not logged in I create the document w/o iCloud sync options enabled.

    Then at startup if iCloud is enabled I perform a scan for documents that need to be migrated and migrate them simply by opening them with the iCloud options enabled. Once opened I close the document as that is enough to get them migrated and scannable via a meta data scan.

    Finally after the migration is done I kick off a new scan for documents.

    It works but it is a bit of a hack.

    Refer to the APManagedDocument commit: 421aaae