2

I have an app that uses a UIManagedDocument (i.e., Core Data) document format. To keep life interesting I am trying to get it to work with iCloud. I have managed to get to the point where I can get one of the following two options:

  1. It works but in the iCloud settings the documents all appear as DocumentMetadata.plist which, needless to say, is pretty confusing
  2. Existing documents display correctly in the iCloud settings but I cannot create new ones

Obviously neither of these is acceptable (though clearly the second is the worst.)

In scenario one, I do not have an document types or Exported UTI types and I set up my NSMetadataQuery to look for the DocumentMetadata.plist (since that's a file and the query can only find things that are considered to be files).

enter image description here

In scenario two, I do have both document types and Exported UTI types defined. In this case the NSMetadataQuery looks for the file extension of my document type. The weird thing in this case is that the saveToURL:completionHandler: method never calls the completion block:

[docToOpen saveToURL:docURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success){
  NSLog (@"This line never gets executed.";
}];

(It does in the first case.)

My guess -- and it is a guess -- is that it uses the same mechanism as NSMetadataQuery to see when the file "guts" of the document gets created but, because my document is a com.apple.package it can't see inside and thinks that it's not finished.

What am I missing? Are you just not supposed to put managed documents in the user visible part of iCloud?

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • Nope you can't just put UIManagedDocument's in the iCloud /Documents directory. Core data only syncs transaction logs via iCloud so it's quite a different process to migrate stores to iCloud. Each device has its own store and imports transaction logs generated on other devices downloaded from iCloud. – Duncan Groenewald Nov 09 '13 at 06:15
  • To get a store to sync via iCloud you need to set the ubiquity name in the persistentStoreCoordinator options, however you can't simply do this to an existing file because in order for it to sync via iCloud you have to build a new copy so that transaction logs get generated in ICloud for creating the database from scratch. Then your app needs to be able to detect new files in iCloud and initiate creation of a local store into which the iCloud transaction logs can be imported. – Duncan Groenewald Nov 09 '13 at 06:23
  • @DuncanGroenewald Thanks for your comment. I don't think my question can be as clear as I intended. I know that you can't just copy a UIManagedDocument to iCloud. My option 1 above works -- I can create a document in the simulator and have it sync to my iPhone. The problem is, as the screenshot shows, those files appear in iCloud settings as DocumentMetadata.plist. – Stephen Darlington Nov 09 '13 at 13:24
  • @DuncanGroenewald and the only solution I've found to that (creating document types) breaks, well, everything else. Note that syncing still works in that second case, but it's not possible to create new documents. – Stephen Darlington Nov 09 '13 at 13:27
  • It's not entirely clear how your app works. Do you want to support multiple user created documents that can be created from any device and ensure they get synced to peer devices? Also you make no mention of setting the ubiquity name when setting up the persistentStoreCoordinator, are you doing this? – Duncan Groenewald Nov 09 '13 at 23:27
  • Hmm, the accepted answer to [this very similar question](http://stackoverflow.com/questions/9026242/nsmetadataquery-ignoring-custom-file-package-type?rq=1) is "This is by design." – Stephen Darlington Nov 10 '13 at 18:48
  • @DuncanGroenewald Yes, I want to support multiple user created documents -- there's not much point in using UIDocument otherwise! I make no mention of setting the ubiquity name as the syncing works _except_ for the name shown in the Setting app. The store coordinator, name, etc. are all so fundamental that it wouldn't work at all without them. – Stephen Darlington Nov 10 '13 at 18:50
  • Ok, just checking. If it's any help I just followed the wwdc2013 207 video recommendation and create the UIManagedDocument in the /documents directory and set the ubiquity options. To detect the presence of new documents I just scan the query results for subdirectories in the iCloud /CoreData directory because these correspond exactly to the ubiquity names of any iCloud files. – Duncan Groenewald Nov 11 '13 at 11:19
  • @DuncanGroenewald Okay, I'll re-watch that video. I don't recall seeing anything about UIManagedDocument but I'd be very happy to be wrong and to have missed something. – Stephen Darlington Nov 11 '13 at 17:20
  • There is no mention of UIManagedDocument but there is mention of creating the documents in the app sandbox rather than in the ubiquity container. – Duncan Groenewald Nov 18 '13 at 02:34

0 Answers0