wgpubs
wgpubs

Reputation: 8261

How to Force Core Data to Create Sqlite Database from Model *Before* Any Data Is Persisted in Application?

Is there a way to force Core Data to create the underlying SQLite database based on your model before you actually create any entities that need to be persisted?

My objective is to define my model in the designer, have the SQLite database created and thus be able to import data into it and/or insert/update data directly thru a tool like SQLite Database browser.

Upvotes: 3

Views: 1248

Answers (1)

Yuji
Yuji

Reputation: 34185

Once your managedObjectContext and persistentStore is ready, just do

[managedObjectContext save:&error];

without putting anything into the context. That'll write the database file which contain no CoreData entity to the file system.

However you can't add entities into the SQLite database backing your CoreData model without using CoreData. The way CoreData translates the object hierarchy into SQLite is an implementation detail, and it's very difficult to create a SQLite file which can be correctly and consistently interpreted by CoreData, without using CoreData itself.

If you want to treat the SQLite file as SQlite file, use SQLite directly, or use a thin wrapper around it like FMDB.

Upvotes: 2

Related Questions