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
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.
Solutions Collect From Internet About “Create Core Data entities dynamically during runtime”
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.
- Xcode 8 shows error that provisioning profile doesn't include signing certificate
- How to make NSSlider be vertical programmatically?
- How to check if JSON is null in swift?
- Swift: Nil is incompatible with return type String
- Swift find superview of given class with generics
- When should I set layer.shouldRasterize to YES
- Dictionary in Swift
- why loading places in iOS using google places api causes lookup place id query error?
- Understanding a malloc_history dump
- How to implement keywords associated function in swift?
- How to compare two UIImage objects
- Weird Facebook ids
- Your account already has a valid Developer ID Application certificate
- Property Declaration and Automatic Backing Storage Allocation
- How can I create a UIView at a certain CGPoint?