Dynamically create Core Data objects at runtime

I need to be able to create new core data objects at runtime. I wrote the code to create objects programmatically, however I cannot add objects at runtime as the model is immutable.

My problem is similar to this post, however there is no satisfactory answer: How dyanmic can create a new object (table) using CoreData model?

The documentation on changing the underlying data model explains:

Managed object models are edited until they are used by a graph manager object (managed object context or persistent store coordinator). This allows you to create or modify them dynamically. However, once the model is in use, it should not be changed. This is a run-time enforcement - when the object manager first fetches data using a model, the whole model becomes impractical. Any attempt to mutate the model or any of its sub-objects after this point throws an exception. If you need to change the model that is in use, create a copy, modify the copy, and then discard objects using the old model.

However, I don't understand what exactly this says - that the entire kernel data model cannot be changed after the persistent store coordinator has been used, or the / etc attributes of individual objects cannot be changed.

To be clear, I don't want to change the attributes of my current objects, I just want to add new objects. It just seems to me that I need to use migration to add new objects.

Any thoughts?

Thank!

+3


source to share


1 answer


The documentation is pretty clear.

  • Copy the model.
  • Apply changes to the new copy.
  • Destroy the old MOC, Persistent Store Coordinator, and any objects created from them.
  • Apply migration if needed.
  • Create a new Core Data Stack (MOC, PSC, etc.) using the updated model.


Migration can be a stagnation point, but it must be workable.

+2


source







All Articles