1

I have used extensively core data in my applications. Generally I need to submit the applications using a pre-populated database.

I generally create zillions of lines of code to populate the database, then I extract it from the applications directory and include it on the bundle. Is there a easy way to do that? Is there any way to populate a core data database created on Xcode using, for example, a CSV file and an external app?

thanks.

Duck
  • 34,902
  • 47
  • 248
  • 470
  • Can't you just write a few lines of code that parses your own csv file and create the inserts based on the information it parses? – Matt C. Jan 03 '13 at 17:07
  • This might help: http://www.raywenderlich.com/12170/core-data-tutorial-how-to-preloadimport-existing-data-updated – Rok Jarc Jan 03 '13 at 17:08
  • @rokjarc - this is other way to do that with code. I use another method that is so extensive than this. I was expecting to simply import the CSV directly. – Duck Jan 03 '13 at 17:15
  • If you are looking for a way to import a csv the way you can with SQL you won't find that for Core Data. However, parsing a csv file and writing a loop that inserts the objects for each line isn't very difficult if you need help let me know, but you seem to be suggesting that this is not the answer you are looking for. If that's the case can you further elaborate on why such an approach would not be ideal? – Matt C. Jan 03 '13 at 17:23
  • i see: i haven't tested this one but is seems promising: http://stackoverflow.com/a/1045961/653513 the first comment (to import from SQL) is missleading... – Rok Jarc Jan 03 '13 at 17:30
  • 1
    @rokjarc that will help build a sql database from a csv file which sql supports doing, but if you have a sql database importing that into core data is just as difficult (if not more so) as importing a csv file to be parsed. – Matt C. Jan 03 '13 at 17:32
  • @MechIntel: my bad. in the mean time i completely forgot about the core data part - it's been a long day. – Rok Jarc Jan 03 '13 at 17:36

2 Answers2

3

The usual way is to create the entries at the first launch. If you want to import data from a CSV file, then you need a parser. I can recommend this one: https://github.com/davedelong/CHCSVParser

artey
  • 1,243
  • 7
  • 10
  • Yep this is the best way. My apps use a similar approach although with json objects rather than a csv file. – Matt C. Jan 03 '13 at 17:44
0

You don't have any easy way to do that with Xcode.

Basically what I do in this kind of case: I build a second target dedicate to pool the database and create object with a fixed persistent store url relative to the workspace in the filesystem.