title: Core Data Inferred Migration – Automatic "lightweight" vs Manual
body: <p>I've updated the model of an existing iPhone app in some simple ways (remove attribute, add attribute, remove index), and can use automatic lightweight migration to migrate the persistent store.</p>
  
  <p>Due to the typical size of the data set, the processing time is not insignificant, and warrants feedback for the user.</p>
  
  <p><code>NSMigrationManager</code> provides a simple but useful <code>migrationProgress</code> value that sends KVO notifications as the migration is performed. That forms the basis of providing feedback, however attempting to use an inferred model (<code>[NSMappingModel inferredMappingModelForSourceModel:destinationModel:error:]</code>) results in drastically different timing for the exact same dataset.</p>
  
  <p>Profile results on an original iPhone (2G), Cache size: 1.785 MB on disk.</p>
  
  <p><strong>Automatic inferred lightweight migration</strong></p>
  
  <pre><code>PROFILE: CacheManager -migrateStore
  PROFILE:   0.6130 (+0.6130) models loaded
  PROFILE:   1.1759 (+0.5629) delegate -CacheManagerWillMigrate:
  PROFILE:   1.2516 (+0.0757) persistent store coordinator loaded
  PROFILE:   5.1436 (+3.8920) automatic lightweight migration completed
  PROFILE:   5.5435 (+0.3999) delegate -CacheManagerDidFinishMigration:withError:
  </code></pre>
  
  <p><strong>Manual inferred migration</strong></p>
  
  <pre><code>PROFILE: CacheManager -migrateStore
  PROFILE:   0.6660 (+0.6660) models loaded
  PROFILE:   1.1471 (+0.4811) inferred mapping model generated
  PROFILE:   1.4046 (+0.2574) delegate -CacheManagerWillMigrate:
  PROFILE:   1.5058 (+0.1013) persistent store coordinator loaded
  PROFILE:   22.6952 (+21.1894) manual migration completed
  PROFILE:   23.1478 (+0.4525) delegate -CacheManagerDidFinishMigration:withError:
  </code></pre>
  
  <p>So, with an inferred model, the manual migration takes over 5 times longer than automatic!</p>
  
  <hr>
  
  <p><strong><em>UPDATE: Model loading</em></strong></p>
  
  <p>Core Data documentation for <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html" rel="nofollow noreferrer"><code>NSPersistentStoreCoordinator</code></a> <a href="http://developer.apple.com/iphone/library/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSPersistentStoreCoordinator_Class/NSPersistentStoreCoordinator.html#//apple_ref/doc/constant_group/Migration_Options" rel="nofollow noreferrer">"Migration Options"</a> says:</p>
  
  <blockquote>
    <p><code>NSInferMappingModelAutomaticallyOption</code></p>
    
    <p>... the coordinator will attempt to
    infer a mapping model if none can be
    found.</p>
  </blockquote>
  
  <p>And that is why the XCode built,compiled &amp; bundled mapping model has to be removed (or just un-targetted) to allow an inferred <strong>and</strong> lightweight migration to happen.</p>
  
  <hr>
  
  <p>It's a big inconsistency, and the <em>lightweight</em> option that <code>NSPersistentStoreCoordinator -addPersistentStoreWithType:configuration:URL:options:error:</code> provides absolutely no indication of progress while processing.</p>
  
  <p>Can anybody provide a supported way to get the <code>migrationProgress</code> values during automatic migration, <strong>OR</strong> a way to configure an inferred mapping model to be as fast during manual processing as automatic?</p>
  
  <hr>
  
  <p><strong><em>UPDATE: Bug Report</em></strong></p>
  
  <p>Spoke to the engineers at WWDC and they've asked for a bug report requesting the <code>migrationProgress</code> for the automatic lightweight migration processing.</p>
  
  <p>I'll update again if the API is updated to add progress reporting..</p>