title: How to force creation of default persistentStore/autosave in NSPersistentDocument
body: <p>I have a document-based Cocoa app using CoreData which can import large amounts of data. Since this import takes some time I do it in the background, but since <code>NSManagedObjectContext</code> is not thread save I used the <code>persistentStoreCoordinator</code> of the <code>managedObjectContext</code> of the document to create a new <code>NSManagedObjectContext</code> in the background thread. 
  Now, when the import is finished I save the background <code>managedObjectContext</code> to notify the document's main-thread <code>managedObjectContext</code> of the changes and merge them. As far as I understand it, this is how one is supposed to do concurrency using CoreData.</p>
  
  <p>But sometimes, the <code>persistentStoreCoordinator</code> does not have a <code>persistentStore</code> when I call <code>[managedObjectContex save:]</code> which causes the save to fail and the app to crash. I'm using autosave and all (basically a mostly unmodifed NSPersistentDocument on OS X 10.8) so I assumed that I don't have to care about how it is saved and it would "just work".</p>
  
  <p>Apparently, this is not the case. 
  I tried forcing an autosave operation, hoping that this would create a persistentStore by calling <code>[self autosaveDocumentWithDelegate:self didAutosaveSelector:@selector(document:didAutosave:contextInfo:) contextInfo:nil];</code> at the end of <code>windowControllerDidLoadNib:</code> in my document subclass, but this doesn't seem to change anything.
  The delegate callback (<code>- (void)document:(NSDocument *)document didAutosave:(BOOL)didAutosaveSuccessfully  contextInfo:(void *)contextInfo</code>) actually states the autosave was successful although neither any of the fileURL or related accessors returns something non-nil nor a persistentStore has been created.</p>
  
  <p>I also thought about calling <code>-(BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError *__autoreleasing *)error</code> myself, but I don't know what URL to use to make it behave just like a normal autosave. 
  I checked <code>backupFileURL</code>, <code>fileURL</code> and <code>autosavedContentsFileURL</code> but all of them are still nil at the end of <code>windowControllerDidLoadNib:</code>. As a matter of fact, they are even nil after <code>writeToURL:...</code> and <code>configurePersistentStoreCoordinatorForURL:...</code> have been called, so I don't have a clue how to get a "correct" URL.</p>
  
  <p>To better understand how and when a persistentStore <strong>is</strong> created I have set breakpoints in <code>writeToURL:...</code> and <code>configurePersistentStoreCoordinatorForURL:...</code>. I noticed that <code>configurePersistentStoreCoordinatorForURL:</code> is called when the application looses frontmost status. But when I just create a new document and try importing something right after it was created (without first switching to another app to cause the persistentStore to be created) it crashes. If I switch to another app, the store is created and everything works fine.
  By the way, the URL used for the persistentStore created this way is always somewhere in the temp directory.</p>
  
  <p>Am I doing something wrong? Don't I have to call [managedObjectContext save:] to notify the other managedObjectContext of the changes?
  How can I force the document to create its temporary persistentStore? Why doesn't the call to <code>autosaveDocumentWithDelegate:...</code> actually save the document (and thus create a <code>persistentStore</code>)?</p>
  
  <p>Related: </p>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/12131338/new-project-new-model-nspersistentdocument-this-nspersistentstorecoordinator-has">New project New Model NSPersistentDocument This NSPersistentStoreCoordinator has no persistent stores</a> Similar problem but the solution there was to not save the context or present an error. I think I have to save the context to notify the second one of the changes and don't want to force the user to save the file manually (after all, that's what autosave is for).</li>
  </ul>