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

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)

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.

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.

Related

NSManagedObject entity class method

I recently realized that NSManagedObject subclasses inherit a class method entity which can be used to obtain a NSEntityDescription for the class. However, I was used to having to specify a context when creating a NSEntityDescription, as with entityForName:inManagedObjectContext:. Is it ok to use the simpler entity method and what context will it be associated with ?
This method is not really documented by Apple.
An NSEntityDescription is not part of a managed object context-- it's part of the managed object model.
When you load a data model, all of the entity descriptions it contains are loaded. The class method +entity works because the entity description was created along with the model object. If you try to call this method before loading the model, it returns nil in Objective-C. (In Swift for some reason it returns a non-optional value, so it's not nil, but if you use it your app will crash. Don't ask me why it's like this.)
You can also use +entityForName:inManagedObjectContext:, as you mentioned. But look at the documentation for that method:
Returns the entity with the specified name from the managed object model associated with the specified managed object context’s persistent store coordinator.
So even though the method takes a managed object context argument, it's still using the managed object model. It's using the context to find the model. The object that you get isn't associated with the context, it's associated with the underlying data model.
These two methods are equally safe. Use whichever works best in your code.

Create Core Data entities dynamically during runtime

I need to be able to create new core data entities during runtime. I've written the code to create the objects programmatically, however, I can't add the entities during runtime as the model is immutable.
My problem is similar to this post, however there is no satisfactory answer: How to dyanmic create a new entity (table) via CoreData model?
The documentation regarding changing the core data model explains:
Managed object models are editable until they are used by an object
graph manager (a managed object context or a persistent store
coordinator). This allows you to create or modify them dynamically.
However, once a model is being used, it must not be changed. This is
enforced at runtime—when the object manager first fetches data using a
model, the whole of that model becomes uneditable. Any attempt to
mutate a model or any of its sub-objects after that point causes an
exception to be thrown. 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.
However, I'm unclear on what exactly this is saying--that the whole core data model can't be changed once the persistent store coordinator has been used or the attributes/etc of the individual entities can't be changed.
To be clear, I do not want to change the attributes of my current entities, I simply want to add new entities. It just seems weird to me to have to use migration to add new entities.
Any thoughts?
Thanks!
The documentation is pretty clear.
Copy the model.
Apply your changes to the new copy.
Destroy your old MOC, Persistent Store Coordinator, and all objects created from those.
Apply a migration, if necessary.
Create a new Core Data Stack (MOC, PSC, etc) using your updated model.
The migration could be a sticking point, but it should be do-able.

What is the difference between mutable and immutable managed object model in Core Data?

After reading RestKit docs about RKManagedObjectStore I was confused about createPersistentStoreCoordinator method because there is a next warning in the description of this method:
**Warning:** Creating the persistent store coordinator will
render the managed object model immutable. Attempts to
use functionality that requires a mutable managed object model
after the persistent store coordinator has been created
will raise an application error.
I didn't understant what does it mean immutable managed object model? I can't found any information about this topic neither in official Core Data docs nor accross the Internet. Can someone give me an explanation of the difference between mutable and immutable managed object models? Why does creating of persistent store coordinator renders immutable managed object model? And what functionality requires a mutable managed object model?
Thanks in advance.
You can change a NSManagedObjectModel (add entities and attributes for example) in code, it is said to be mutable. But once you attach your object model to an persistent store coordinator you are not allowed to change it anymore - it has become immutable.
This also is described in the documentation for NSManagedObjectModel:
Editing Models Programmatically Managed object models are editable
until they are used by an object graph manager (a managed object
context or a persistent store coordinator). This allows you to create
or modify them dynamically. However, once a model is being used, it
must not be changed. This is enforced at runtime—when the object
manager first fetches data using a model, the whole of that model
becomes uneditable. Any attempt to mutate a model or any of its
sub-objects after that point causes an exception to be thrown. 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.

Simperium tried to send object changes for nil key

What does Simperium tried to send object changes for nil key mean and how can I debug / fix it?
This will occur if an object doesn't have a simperiumKey. Some things to check:
Does your entity inherit from a parent entity in your model file (usually SPManagedObject) that has the simperiumKey attribute of type String?
Is the class for your entity set to SPManagedObject? Or, if you're using a custom subclass, have you updated the header file for your subclass to inherit from SPManagedObject instead of NSManagedObject?
If you're manually adding the simperiumKey attribute to a model (for example, to micromanage your tables: Inherit from SPManagedObject), you may also need to manually add the simperiumKey and ghostData variables to your custom subclass if you have one.
We'll also improve the log message to give you a better indication of what is happening in this case.

Automapper not mapping entities that are newly added in Entity Framework 4.1 context

I am lazy loading my entities and when I make an initial call to pull a list of entities I am seeing the System.Data.Entity.DynamicProxies....type. Which is fine and my entities map to my DTOs just fine. The issue I am having is that when I add a new item to my context it is being pulled fromt he context and not the database. So, I see the list being pulled back with the System.Data.Entity.DynamicProxies types and an my new item taht is the actual type of object.
So, for example I may have a list like this:
System.Data.Entity.DynamicProxies.Contact...
System.Data.Entity.DynamicProxies.Contact...
System.Data.Entity.DynamicProxies.Contact...
MyNameSpace.Contact
I created a custom type converter and noticed that the DynamicProxy types have a source value, but the MyNameSpace.Contact does not have a source value. So, it is not getting mapped in my type converter.
You can call the CreateObject/Create method in your ObjectSet/DbSet when you are creating the entity without using the constructor.
var newContact = context.Contacts.Create();
context.Contacts.Add(newContact);
The returned object is a proxy that supports lazy loading.

Resources