Do you have an idea for an app but lack the programming knowledge to begin build it? In this weekly blog series, I will take you, the non-programmer, step by step through the process of creating apps for the iPhone, iPod touch, and iPad. Join me each week on this adventure, and you will experience how much fun turning your ideas into reality can be! This is Part 18 of the series. If you are just getting started now, check out the beginning of the series here. (This post has been updated to iOS 7.1.)
We've been discussing Core Data in my last several posts and now you should understand the basics of:
- Adding Core Data to a project
- Designing entities in a data model
- Generating entity classes from a data model
- How to retrieve, update, and delete entities
- Creating business controller classes for each entity class
- Using business controllers with view controllers
This week we're going to take a look at Core Data behind the scenes and examine the database that is automatically created for you the first time you try to retrieve entities. If you don't have it already, you can get a copy of the latest project from this link.
Examining the SQLite Database
When you run an app in the Simulator, a folder is created for you on your development computer's hard drive that contains the compiled app and its related files. To see this folder:
- Launch the Mac OS X Finder App (the one with the smiley face);
- In the Finder menu, click Go, and then hold down the option key. This adds Library to the list of folders as shown in Figure 1;
|Figure 1 - Hold down the Option key to add Library to the list of folders.|
- Select the Library folder from the menu to display the contents of this folder in Finder;
- Next, drill down into the Application Support folder, and then down into the iPhone Simulator folder. You will see one or more subfolders, one for each version of the iPhone Simulator you have installed on your computer (Figure 2);
|Figure 2 - The iPhone Simulator folders|
- Next, drill down into the folder for the latest version of the iPhone Simulator (in this case, the 7.1 folder), and then drill down into the Applications subfolder. You will now see a list of folders with unique identifier names as shown in Figure 3. The unique identifiers on your machine will be different than those shown in this figure (that's what makes them unique). Each of these subfolders represents an app that you have run in the Simulator;
|Figure 3 - The Applications folders|
- Before drilling down into the next level, set the view mode of Finder to Cover Flow. To do this, click the Finder toolbar button shown in red in Figure 4;
|Figure 4 - Select Finder's Cover Flow viewing mode.|
- Before going further, let's bookmark the Applications folder so it's easy to get back here. Select the Applications folder and drag it to the left side of the Finder window under Favorites (Figure 5);
|Figure 5 - Add the Applications folder to Favorites.|
- Now let's try to find the iAppsReview project folder. With the Finder set to Cover Flow, select the first unique identifier folder in the list, and you will see a file with a .app extension along with Documents, Library, and tmp folders. Press the down arrow until you see the iAppsReview.app file as shown in Figure 6;
|Figure 6 - Look for the iAppsReview.app file.|
- When you find the folder that contains the iAppsReview.app file, select the Documents subfolder and you will see the iAppsReview.sqlite database file (Figure 7) along with its associated .sqlite-shm and .sqlite-wal files.
|Figure 7 - The .sqlite file is located in the Documents folder.|
To examine the contents of the .sqlite file, you need a special app, which is discussed in the next section.
Firefox and the SQLite Manager
I have found that one of the best tools for viewing a SQLite database is the SQLite Manager add-on for the Firefox web browser. I'll show you how to get this tool on your development machine.
- If you don't already have the Firefox web browser installed on your Mac, go to this link to download and install Firefox:
- After installing Firefox, go to the following link to download and install SQLite Manager:
- Now that you have both Firefox and SQLite Manager installed, launch Firefox (you should be able to find it in your Applications folder on your Mac);
- After Firefox successfully launches, go to the Firefox Tools menu and select SQLite Manager. This displays the SQLite Manager window (Figure 8);
|Figure 8 - The SQLite Manager window|
- To open the app's SQLite database files, click the Connect Database toolbar button at the top of the SQLite Manager window (Figure 9);
|Figure 9 - Opening a database in SQLite Manager|
- On the left side of the Select SQLite Database dialog, click the Applications folder you added to Favorites in the previous section. On the right side of the dialog, drill down into the folder that contains iAppsReview files and then drill down into the Documents folder to select the iAppsReview.sqlite database (Figure 10);
|Figure 10 - Select the iAppsReview.sqlite file.|
- Click the Open button to open the database in SQLite Manager. This displays the iAppsReview database files in the panel on the left (Figure 11).
|Figure 11 - iAppsReview Database tables|
You don't need to know all the details about what each of these tables does. In fact, one of the main goals of Core Data is to hide these details from you. However, it's good to have a basic understanding of the underlying mechanics in case something should go wrong.
There are three tables, ZAPPCATEGORYENTITY, ZREVIEWENTITY, and ZUSERENTITY that have the same name as the entities that you added to the entity data model, but with a Z prefix;
- Expand the table nodes and you will see the columns shown in Figure 12. Z_PK is the primary key (unique identifier) for the entity, Z_ENT is the entity ID (every entity of a particular type has the same entity ID), and Z_OPT indicates the number of times an entity has been changed (starting with 1 when it is initially added to the database). All of the other columns are attributes you added to the entities in the entity data model with a Z prefix;
|Figure 12 - iAppsReview table columns|
- Let's add records to the database so that we can have something to view in our app. Note that you shouldn't normally do this. We're doing it in this post to see first hand how a database works.
In the panel on the left in the SQLite Manager, click on ZAPPCATEGORYENTITY to select it. Then, on the right side of the panel make sure the Browse and Search tab is selected, and then click the Add button. This displays the dialog shown in Figure 13;
|Figure 13 - Adding an App Category record|
- Enter 1 in the Z_ENT, Z_OPT, and ZCATEGORYID boxes, and in the ZNAME box, enter the text Entertainment as shown in Figure 14;
|Figure 14 - Adding an Entertainment category|
- Click the OK button. This displays the confirmation dialog shown in Figure 15;
|Figure 15 - The new record confirmation dialog|
- Click the OK button to add the new record to the database. When you do this, the New Record dialog is redisplayed with a confirmation message at the top as shown in Figure 15. As stated in this message, if you press Cancel, you will exit the dialog. Otherwise, you can add another new record;
|Figure 16 - Record inserted successfully!|
- To add the next record, press the tab key until the ZCATEGORYID box has focus and its contents are highlighted (without changing any other values). Type the number 2, then tab to the ZNAME box and enter the text Games (which deletes the existing text) and press OK. In the confirmation dialog, press OK to save the record;
- Now let's add just one more category record. Tab down to the ZCATEGORYID box again, and this time enter the number 3. Next, tab to the ZNAME box, enter the text News, and click the OK button to save the new record. Afterwards, press Cancel to exit the dialog.
At this point you should now have three app category records in the database.
Test Driving the App
Now that you have app category records in the database, let's run the app again and see how this data looks at run time.
- At the top-left corner of the Xcode window, set the scheme to iPhone Retina (3.5-inch) or (4-inch).
- Click Xcode's Run button;
- When the app appears in the Simulator, click the Write a Review row at the top of the iAppsReview screen;
- Click the App Category row at the top of the Write Review scene and you should see the three categories you just entered in the database as shown in Figure 17! If you are like most people, it's pretty exciting to see the app displaying entities that it retrieved from the database on the device.
|Figure 17 - The app categories at run time!|
If you want to see more records in the table view, go ahead and add the following category rows to the database just as you did in the previous section and then run the app in the Simulator to see the new records:
At this point you have done much of the work necessary for adding Core Data to the iAppsReview project. In my next post we will enhance the project to create and update other entities and I'll even show you how to ship a database with your app so that your users have a database pre-populated with data.