title: Can create, but not open, a UIManagedDocument with iCloud support
body: <p>I am creating a new UIManagedDocument with iCloud support as follows:</p>
  
  <ol>
  <li>Alloc and init with local sandbox URL</li>
  <li>Set persistent store options to support iCloud: ubiquitousContentNameKey and ubiquitousContentURL. The name I'm generating uniquely and the URL is pointing to my ubiquityContainer / CoreData.</li>
  <li>Save locally to sandbox with UIManagedDocument's saveToURL method.</li>
  <li>In completion handler, move to iCloud with FileManager's setUbiquitous method.</li>
  </ol>
  
  <p>So far, this dance works. (Well, sort of). After I call setUbiquitous, I get an error that says it WASN'T successful, however the document moves to the cloud. When it's done, I have a new document in the cloud. This appears to be a bug, as I've been able to replicate it with others' code.</p>
  
  <p>I'm actually generating this document in a "Documents View Controller," which lists all of the documents in the cloud. So when this new document's final completion handler is finished, it shows up in the table view thanks to an NSMetadataQuery. So far, pretty standard usage I think.</p>
  
  <p>To edit a document, the user taps and goes to a "Single View Document View Controller."</p>
  
  <p>In this view controller, I need to "reopen" the selected document so the user can edit it.</p>
  
  <p>So I go through series of steps again:</p>
  
  <ol>
  <li>Alloc / init a UIManagedDocument with a fileURL -- this time, the URL is from the cloud.</li>
  <li>Set my persistent store options, same as step 2 above, with same settings.</li>
  </ol>
  
  <p>Now, I ATTEMPT step 3, which is to open the document from disk, but it fails. The document is in a state of "Closed | SavingError" and the attempt to open fails.</p>
  
  <p><strong>Does anyone know why my document would create OK, move to the cloud OK, but then fail to open on an immediate subsequent attempt? (Really, an attempt within that launch of the app - see below). Specifically, what would make a UIManagedDocument instance be created but in a closed, non-openable state?</strong></p>
  
  <p><strong>Interestingly enough, if I quit the app and launch again, I can tap and reload the document and edit it.</strong></p>
  
  <p>And very occasionally I can create, then open, and edit very briefly, say insert one managedobject, and then it goes into this close | saving error state.</p>
  
  <p>ERROR INFO:</p>
  
  <p>I've subclassed UIManagedDocument and overrode the -handleError: method to try and get more information, and here's what I get (along with some other debugging logs I put in):</p>
  
  <p>2012-10-05 14:57:06.000 Foundations[23687:907] Single Document View Controller View Did Load. Document:  fileURL: file://localhost/private/var/mobile/Library/Mobile%20Documents/7PB5426XF4~com~howlin~MyApp/Documents/New%20Document%2034/ documentState: [Closed]</p>
  
  <p>2012-10-05 14:57:06.052 MyApp[23687:907] Document state changed. Current state: 5  fileURL: file://localhost/private/var/mobile/Library/Mobile%20Documents/7PB5426XF4~com~howlin~MyApp/Documents/New%20Document%2034/ documentState: [Closed | SavingError]</p>
  
  <p>2012-10-05 14:57:06.057 Foundations[23687:5303] UIManagedDocument error: The store name: New Document 34 is already in use.
      Store URL: file://localhost/private/var/mobile/Library/Mobile%20Documents/7PB5426XF4~com~howlin~MyApp/Documents/New%20Document%2034/StoreContent.nosync/persistentStore
      In Use Store URL: file://localhost/var/mobile/Applications/D423F5FF-4B8E-4C3E-B908-11824D70FD34/Documents/New%20Document%2034/StoreContent.nosync/persistentStore</p>
  
  <p>2012-10-05 14:57:06.059 MyApp[23687:5303]   {
      NSLocalizedDescription = "The store name: New Document 34 is already in use.\n\tStore URL: file://localhost/private/var/mobile/Library/Mobile%20Documents/7PB5426XF4~com~howlin~MyApp/Documents/New%20Document%2034/StoreContent.nosync/persistentStore\n\tIn Use Store URL: file://localhost/var/mobile/Applications/D423F5FF-4B8E-4C3E-B908-11824D70FD34/Documents/New%20Document%2034/StoreContent.nosync/persistentStore\n";
      NSPersistentStoreUbiquitousContentNameKey = "New Document 34";
  }</p>
  
  <p>The error seems to think I'm it create a store that already exists on the subsequent opening. Am I now supposed to set those iCloud option on the persistent store on a second opening? I've tried that approach and it didn't work either.</p>
  
  <p>I've studied the Stanford lectures on UIManagedDocument and don't see what I'm doing wrong.</p>
  
  <p>Here's my method to create the doc and move to cloud:</p>
  
  <pre><code>- (void) testCreatingICloudDocWithName:(NSString*)name
  {
  
      NSURL* cloudURL = [self.docManager.iCloudURL URLByAppendingPathComponent:name isDirectory:YES];
  
      NSURL* fileURL = [self.docManager.localURL URLByAppendingPathComponent:name];
  
  
      self.aWriting = [[FNFoundationDocument alloc] initWithFileURL:fileURL];
  
  
      [self setPersistentStoreOptionsInDocument:self.aWriting];
  
      [self.aWriting saveToURL:fileURL forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL success) {
          if (success == YES) {
  
              dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
                  //create file coordinator
                  //move document to icloud
  
                  NSFileCoordinator* fileCoordinator = [[NSFileCoordinator alloc] initWithFilePresenter:nil];
                  NSError* coorError = nil;
                  [fileCoordinator coordinateWritingItemAtURL:cloudURL options:NSFileCoordinatorWritingForReplacing error:&amp;coorError byAccessor:^(NSURL *newURL) {
  
                      if (coorError) {
                          NSLog(@"Coordinating writer error: %@", coorError);
                      }
  
                      NSFileManager* fm = [NSFileManager defaultManager];
                      NSError* error = nil;
                      NSLog(@"Before set ubiq");
                      [fm setUbiquitous:YES itemAtURL:fileURL destinationURL:newURL error:&amp;error];
                      if (!error) {
                          NSLog(@"Set ubiquitous successfully.");
                      }
                      else NSLog(@"Error saving to cloud. Error: %@", error);
  
  
                      NSLog(@"State of Doc after error saving to cloud: %@", self.aWriting);
                  }];
  
  
  
              });
  
          }
      }];
  
  }
  </code></pre>
  
  <p>Here's where I set options for iCloud on the persistentStore:</p>
  
  <pre><code>- (void)setPersistentStoreOptionsInDocument:(FNDocument *)theDocument
  {
      NSMutableDictionary *options = [NSMutableDictionary dictionary];
      [options setObject:[NSNumber numberWithBool:YES] forKey:NSMigratePersistentStoresAutomaticallyOption];
      [options setObject:[NSNumber numberWithBool:YES] forKey:NSInferMappingModelAutomaticallyOption];
  
      [options setObject:[theDocument.fileURL lastPathComponent] forKey:NSPersistentStoreUbiquitousContentNameKey];
      NSURL* coreDataLogDirectory = [self.docManager.coreDataLogsURL URLByAppendingPathComponent:[theDocument.fileURL lastPathComponent]];
      NSLog(@"Core data log dir: %@", coreDataLogDirectory);
      [options setObject:coreDataLogDirectory forKey:NSPersistentStoreUbiquitousContentURLKey];
  
      theDocument.persistentStoreOptions = options;
  }
  </code></pre>
  
  <p>And here's where I try to reopen it:</p>
  
  <pre><code>- (void) prepareDocForUse
  {
  
      NSURL* fileURL = self.singleDocument.fileURL;
  
      if (![[NSFileManager defaultManager] fileExistsAtPath:[fileURL path]]) {
          NSLog(@"File doesn't exist");
              }
      else if (self.singleDocument.documentState == UIDocumentStateClosed) {
          // exists on disk, but we need to open it
          [self.singleDocument openWithCompletionHandler:^(BOOL success) {
              if (!success) {
                  NSError* error;
                  [self.singleDocument handleError:error userInteractionPermitted:NO];
              }
              [self setupFetchedResultsController];
          }];
      } else if (self.singleDocument.documentState == UIDocumentStateNormal) {
          // already open and ready to use
          [self setupFetchedResultsController];
      }
  
  }
  </code></pre>