title: How to change managed object class name before fetching
body: <p>I have a Swift app that uses CoreData. I created <code>List</code> entity with class <code>MyAppTarget.List</code>. Everything is configured properly in .xcdatamodeld file. In order to fetch my entities from persistent store, I am using <code>NSFetchedResultsController</code>:</p>
  
  <pre><code>let fetchRequest = NSFetchRequest()
  fetchRequest.entity = NSEntityDescription.entityForName("List", inManagedObjectContext: managedObjectContext)
  fetchRequest.sortDescriptors = [ NSSortDescriptor(key: "name", ascending: true) ]
  let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: managedObjectContext, sectionNameKeyPath: nil, cacheName: "ListFetchedResultsControllerCache")
  </code></pre>
  
  <p>and it works like expected, returning array of <code>MyAppTarget.List</code> objects when fetching.</p>
  
  <p>However, I would like to use it inside another target, for unit testing. I added <code>List</code> class to <code>MyUnitTestTarget</code>, so I can access it inside the unit test target. The problem is that the fetched results controller returns <code>MyAppTarget.List</code> objects, not the <code>MyUnitTestTarget.List</code> objects. In order to make the <code>List</code> entity testable, I have to make it public along with all methods that I need to use and I would like to avoid this.</p>
  
  <p>I tried to change the <code>managedObjectClassName</code> property on <code>NSEntityDescription</code>:</p>
  
  <pre><code>fetchRequest.entity.managedObjectClassName = "MyUnitTestTarget.List"
  </code></pre>
  
  <p>but it generates exception:</p>
  
  <blockquote>
    <p>failed: caught "NSInternalInconsistencyException", "Can't modify an immutable model."</p>
  </blockquote>
  
  <p>The <a href="https://developer.apple.com/library/prerelease/ios/documentation/Cocoa/Reference/CoreDataFramework/Classes/NSEntityDescription_Class/index.html" rel="nofollow">documentation</a> states that </p>
  
  <blockquote>
    <p>[...] once a description is used (when the managed object model to which it belongs is associated with a persistent store coordinator), it must not (indeed cannot) be changed. [...] If you need to modify a model that is in use, create a copy, modify the copy, and then discard the objects with the old model.</p>
  </blockquote>
  
  <p>Unfortunately, I don't know how to implement this flow. I wonder if there is a way to change the managed object class name in runtime, before fetching the entities with <code>NSFetchedResultsController</code>?</p>