If you’re writing an iOS app that uses Core Data then you may well want to ship it with an initial database (which potentially gets over the air updates later on).
On iOS, CoreData stores always use sqlite3 as their backend. You could create a sqlite database directly, but you’d have to reverse engineer the way apple uses sqlite, ensure that you use the same name manging for table and column names, generate the same meta data used for persistent store migration etc. Too brittle for my liking.
Luckily both RubyCocoa and MacRuby allow you to access the Core Data framework from a ruby script. The former is bundled with Mac OS X since 10.5 (you will need to use the system ruby). RubyCocoa has some clunkier syntax because it doesn’t have the benefit of the extensions to ruby, however at the moment MacRuby doesn’t quite work with Active Record, which is where I was getting my seed data from. If your data is coming from else where, this may not be a problem. Other than the slight syntax differences around the handling of Objective-C’s sort-of-named-arguments the code is the same
The RubyCocoa version looks like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
|
Having done this, you use it like this
1 2 3 4 |
|
The too arguments are the path to the file you want to store the data in, and the path to your model file.
Your Mom’s a data model
You may be wondering what a .mom file is. In XCode you work with a .xcdatamodel file. When you build your app, this is compiled down into a .mom file. You can also do it your self by running
/Developer/usr/bin/momc my_model.xcdatamodel my_model.mom