Populating Core Data with External Data

Hi,

I was recently able to request for my library to purchase Core Data by Tutorials, V 3.1. I have only skimmed through it currently and will dive more in after learning more about Swift and app development.

Is there a way to populate the Core Data data from an external source? Basically I have a spreadsheet with columns for “Min”, “Max”, “1”, “2”, and “3”. There are a few hundred rows of data (there could be a few thousand rows if I combine apps). How would I easily put this data into core data? The data only needs to be referenced by the app to populate a number from the columns 1, 2, or 3. There will be no place for the user to add or change data in the app. It’s basically an information database behind the scenes which has a number pulled from it. It seems like it would be fairly simple, but it would take a long time to put the data in manually (I’m not sure how I would do that either though), and then deploy the app to the app store.

Hi @tsukaska, and thanks very much for posting your question!

If you have a spreadsheet that is currently holding your data, one suggestion would be to use the spreadsheet to convert the data into a .csv file. After doing that, import that file into your Xcode project, and then iterate over that file, and create model object subclasses, and populate your Core Data with those entities. What you would do after that is run the app on your simulator, and then inside your project file that is running on your simulator, remove the .sqlite file. You’ll notice that this .sqlite file will contain all the records that you’ve populated it with from your .csv file. Bring this .sqlite file into your Xcode project (and rename it to something else), and then reference it inside your Core Data boiler plate code where you reference the .sqlite file. This will allow you to use the pre populated .sqlite file in your app, and would then be able to ship it to the app store.

Here is a link to the Apple Docs which provides instructions on how to do this:

Creating a Default Data Store in Core Data

I hope this helps!

Take care :slight_smile:

@tsukaska I realize that this may be a bit outdated, but I do believe the information provided is somewhat relevant to your question:

Preloading data into Core Data

I hope this helps!

I apologize for the late reply, thank you for your promptness and follow-up! I need to figure out the nuances of it all still. It seems like I would be just putting the .csv file into Xcode to have Xcode turn it into a .sqlite file by doing the iterate, model object, subclasses, etc? Instead of doing those things to populate the Core Data to get a .sqlite file, could I use DB Browser for SQLite to convert my .csv file to a .sqlite? Or is there some sort of special ‘formatting’ which would happen if I have Xcode create it?

@tsukaska That too is also possible. As long as the SQLite file is correctly formatted, and you reference the file correctly inside your Xcode project, you should be fine. The data manipulation that you do inside your project is essentially a longer process of essentially doing what you described. The reason why I suggested my approach is that when you’re dealing with large data sets, manually entering the data could take a while. However, if you have a SQLite browser which can convert .csv files into SQLite format, then you should be fine. :slight_smile:

All the best!

1 Like