0

I am trying to add a new entity in NSManagedObjectModel in my NSIncrementalStore Subclass. I am doing this in loadMetadata method but it keeps throwing this exception on the last line. See Code Below

"NSInternalInconsistencyException" Entities for a configuration must already be in the model

Code

var model:AnyObject=(self.persistentStoreCoordinator?.managedObjectModel.copy())!
var newEntity=NSEntityDescription()
newEntity.name="newEntity"
newEntity.managedObjectClassName="newEntity"

var entities=model.entitiesForConfiguration(self.configurationName)
entities?.append(newEntity)

model.setEntities(entities!, forConfiguration: self.configurationName)
Icaro
  • 14,585
  • 6
  • 60
  • 75
Nofel Mahmood
  • 898
  • 7
  • 12

2 Answers2

0

You cannot modify a model after it has been added to a persistent store coordinator. The only time you can manipulate the model is just after initialization and before applying it to the NSPersistentStoreCoordinator.

Marcus S. Zarra
  • 46,571
  • 9
  • 101
  • 182
  • The code above is before I add it to NSPersistentStoreCoordinator. It still throws this exception. I am actually trying to make a copy of the model and add it to NSIncrementalStore's private instance of NSPersistentStoreCoordinator ! It works when I add any new attributes to entities but not when I try to add a new entity ! – Nofel Mahmood Jun 08 '15 at 06:36
  • In the code above you are retrieving the model from the PSC so that indicates to me that the model has already been added to the PSC and therefore has already been "touched". – Marcus S. Zarra Jun 08 '15 at 20:42
  • Can you isolate this in a test case that I can play with? – Marcus S. Zarra Jun 08 '15 at 20:42
  • I am making a mutable copy of the "touched" model ! – Nofel Mahmood Jun 09 '15 at 07:41
  • @NofelMahmood I believe you have found a bug, did you report this through radar? – quellish Oct 08 '15 at 01:01
  • I do not see this as a bug in Core Data. You cannot take an immutable model and make it mutable. This is not like other muta blue objects on Cocoa. If anything, your ability to add properties is a bug. Would still like to see a test case so that we can see what you are trying to do. – Marcus S. Zarra Oct 11 '15 at 18:37
  • anyone knows how to add entities in model i am trying but i getting error for configuration of model – chetan panchal Nov 10 '17 at 07:39
  • @chetanpanchal It is inappropriate to post a question in comments. Please open your own question directly. – Marcus S. Zarra Nov 13 '17 at 23:28
0

The documentation is unclear about this, but before calling setEntities:forConfiguration: the entities being set must already exist in the managed object model's entities array. This is because this method actually assigns existing entities in the model to a particular configuration.

The solution here is to make a mutable copy of the entities array, add your entities to it if they do not exist, and then set the managed object model's entities array to an immutable copy of the modified array. After that point you can invoke setEntities:forConfiguration:.

It would be worth filing a radar bug report on this behavior.

quellish
  • 21,123
  • 4
  • 76
  • 83