Programmatically create a new ios-core data table

Can Core Data allow me to create a new table programmatically? or if I need, I need to use SQLite directly.

thanks

+6
source share
3 answers

From a CoreData point of view, you really don't create new tables, because database tables are just one possible persistence store associated with the underlying data model.

However, you can create new core data objects programmatically using the NSEntityDescription class. In the documentation for the NSEntityDescription class, you will find the following:

Entity descriptions are editable until they are used by an object graph manager. This allows you to create or modify them dynamically. However, once a description is used (when the managed object model to which it belongs is associated with a persistent store coordinator), it must not (indeed cannot) be changed. This is enforced at runtime: any attempt to mutate a model or any of its sub-objects after the model is associated with a persistent store coordinator 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. 

I never tried changing it at runtime, so I'm not sure how well this works if you have an existing SQLite save store, if at all. But it's probably worth playing with NSEntityDescription to see if it is close to what you are trying to do.

+4
source

You typically create a managed object model graphically using the Xcode data model design tool. (If you want, you can create the model programmatically at runtime

Master Data Programming Guide

0
source

However, you can:

  • Create an object model context (outside the current one you are using / using)
  • Create one or more objects
  • Create SEPARATE persistent storage for this model
  • Save objects, etc.
  • Close the store when done.

You cannot change models on the fly, as they are pretty much fixed when they get pulled into the runtime.

0
source

Source: https://habr.com/ru/post/917406/


All Articles