View your core data SQLite database

I am currently writing an ios app that uses a medium size pre-populated core data database. Having worked with mysql previously along side php and java I have to say that the way core data is implemented makes it very powerful and great to use for object persistence.
In my app I am opening a plist file as a dictionary and looping through each entry to create the core database within the app.
I am also using a UITableView and a NSFetchedResultsController to display the table in the app which is another strength of core data as it automatically reloads the table when the data changes.
The first time I ran the app and viewed the table some of the objects had the wrong section  headers and also some of the section titles were incorrect.
When I checked the plist files the entires that were showing up incorrectly were fine in the plist file.  In mysql I would have just done a select * to see all the entires in the table to see what the database looked like but wasn’t sure how to do this with core data.

This is what I ended up doing to find the incorrect entities:

I set the SQL debug flag in xcode. In xcode 4 this can be done from the Product menu by selecting “Manage Schemes” and then editing the current scheme you are using. Select the “Run “phase and then select the “Arguments” tab. Here you will see options to set arguments and environment variables. Add the code below as an argument:

-com.apple.CoreData.SQLDebug 1

I then ran my app in the ios simulator which gave me the location of the sql file on my computer. I copied the location (minus the sql file name) and pasted it into the “Go to Folder” field in the “Go” menu of finder then copied the .sql file into my user folder.

I then downloaded SQLite Database Browser which is a free open source SQLite browser (the title is pretty explanatory really) and opened my .sql file and viewed the tables.

I found the entires which were incorrectly entered and fixed them in my .plist file that populates my core data database which in tern fixed the problem with the section headers I was having. All without too many (more) hairs turning grey!

Leave a Reply

Your email address will not be published. Required fields are marked *


*