title: How do you correctly update a model in Xcode4 without corrupting it?
body: <p>I never had any problems with Xcode3, but with Xcode4 I'm getting Apple's code failing approx 1 time in 3 when I update a core data model, with the dreaded "Persistent store migration failed, missing source managed object model." error.</p>
  
  <p>Here's my setup (how I configured the project to auto-migrate):</p>
  
  <ol>
  <li>NSPersistentDocument, from Apple's template</li>
  <li>Override Apple's model-loading method, and the ONLY thing I do is to provide the two flags in the storeOptions Dictionary, which turn on auto-migration</li>
  </ol>
  
  
  
  <pre><code>-(BOOL)configurePersistentStoreCoordinatorForURL:(NSURL *)url ofType:(NSString *)fileType modelConfiguration:(NSString *)configuration storeOptions:(NSDictionary *)storeOptions error:(NSError **)error
  {
  
      NSMutableDictionary *newOptions = nil;
  
      if( storeOptions != nil )
          newOptions = [NSMutableDictionary dictionaryWithDictionary:storeOptions];
      else
          newOptions = [NSMutableDictionary dictionary];
  
      [newOptions setValue:@"YES" forKey:NSMigratePersistentStoresAutomaticallyOption];
      [newOptions setValue:@"TRUE" forKey:NSInferMappingModelAutomaticallyOption];
  
      BOOL success = FALSE;
  
      success = [super configurePersistentStoreCoordinatorForURL:url ofType:fileType modelConfiguration:configuration storeOptions:newOptions error:error];
  
      return success;
  }
  </code></pre>
  
  <p>Here's the process I've been using (which is already working around 1 bug in Xcode4!)</p>
  
  <ol>
  <li>Select the model (named "something.xcdatamodel" in Xcode4, with a twisty on the left)</li>
  <li>Go to Editor menu, select "Add new model version..."</li>
  <li>Name the new version 1 integer higher than last - e.g. if previous was "4" name the new one "5"</li>
  <li>In the right-hand pane, change the current model version to the newly-created one</li>
  <li><em>workaround for XCode4 bug</em>: select any file, then select the newly-created model. If you do not, Xcode shows the selection on the newly-created model, but will edit the previous model instead, which definitely corrupts everything in CoreData</li>
  <li>Edit your model; in this case, I'm adding a new attribute to an existing entity</li>
  <li>Save. Build. Run. ... CRASH.</li>
  </ol>
  
  <p>Except, as I said, approx 2 times in 3 this works correctly. Once it works once, it's (obviously) fine - the lightweight migration is complete, the next save saves in the new model version.</p>
  
  <p>So I'm guessing there's something I'm doing wrong in the above steps, but I've been through the docs 5 or 6 times and can't see anything obvious. Doesn't help that NSPersistentDocument docs are all out of date - but I've done lightweight migration on iPhone lots of times too, so I'm reasonably confident with doing this, and it seems right to me.</p>
  
  <hr>
  
  <p>Other things I've tried/checked:
   - <a href="https://stackoverflow.com/questions/3146515/iphone-core-data-lightweight-migration-cocoa-error-134130-cant-find-model-for-s/6450961#6450961">iPhone Core Data Lightweight Migration Cocoa error 134130: Can&#39;t find model for source store</a> (nope; only the root xcdatamodel was being included)</p>