user7631123
user7631123

Reputation:

How to set configuration of NSManagedObjectModel while creating Entites programatically.

I'm creating entities in core data's managed object model programmatically using SWIFT.

When I create an array of NSEntityDescription and pass it to

let managedObjectModel : NSManagedObjectModel = NSPersistentContainer(name: "Nequore").managedObjectModel

managedObjectModel.setEntities(arrEntities, forConfigurationName:"Default")

I'm facing crash related to model's configuration which states that

*** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Entities for a configuration must already be in the model.'

Thank you for your time and consideration.

Upvotes: 1

Views: 841

Answers (1)

Jerry Krinock
Jerry Krinock

Reputation: 5030

I think that this crash is explained in Apple documentation for NSManagedObjectModel. Read the paragraph Editing Models Programmatically.

Your statement let managedObjectModel is creating a persistent container (Core Data Stack), including a managed object model, which is probably empty (has no entities), and I'm guessing that creating the persistent container is using the model in such a way that, per that documentation, it should no longer be edited. Try to create your managed object model first, complete with all entities, and then pass that model to NSPersistentContainer init(name:managedObjectModel:).

Upvotes: 2

Related Questions