Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

programmatically create new table with ios-core data

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

thanks

like image 566
shebelaw Avatar asked Jun 05 '12 00:06

shebelaw


1 Answers

From a CoreData perspective, you don't really create new tables because database tables are only one possible type of persistence store associated with the core data model.

You can, however, create new core data entities programatically using the NSEntityDescription class. In the NSEntityDescription class documentation you will find this:

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've never tried to modify one at runtime, so I'm not sure exactly how well this works when you have an existing SQLite persistence store, if at all. But it's probably worth playing around with NSEntityDescription to see if it gets you close to what you are trying to do.

like image 182
Tim Dean Avatar answered Oct 06 '22 00:10

Tim Dean