Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
At this point, we have a working SQLite 3 database wrapper class called BWDB and a working SQLite 3 database called bwrss.db. Now we want to take these components and make an Objective-C class for using these components in iOS. So we will start by making a working copy of our project so far. So you can make a working copy of your results from movie three or like I am going to do here, I am just going to make a working copy from Testbed-03-done, and I am going to rename this to Testbed-05-working.
I am going to open up the project here in Xcode by double-clicking on the Xcode project file. Here we've got our BWDB Library, and you notice our testdatabase method here in our ViewController. I'm going to come right back over here to the Finder, and I am going to--first of all, I'm going to read-in our database. This is the database we created in the last movie, bwrss.db. I'm just going to drag this over here into our target, and you notice that I get this dialog box.
I want to make sure Copy is checked, and I also want to make sure Add to Target is checked for our Testbed target. I'll click Finish, and there's the file there, right next to the dynamic library. And now I'm going to come back out here in the Finder, I am going to Libraries, and I am going to do the same thing. I am going to drag this RSSDB folder, and we'll take a look at this one as we import it. This is going to go in our Testbed group here inside of the target. I'll let go of that and make sure that Copy and Create Groups and Testbed are all selected, and there is our RSSDB code.
If we look up here at the Header File, we see that what we are doing is we're extending the BWDB class, with a new class called RSSDB. This new class has got a bunch of methods in it. This is all specific for dealing with feeds and items. If we come over here into the m file, the implementation file, you see we've got a new constructor that calls the super constructor. That's the constructor of the parent class, the inherited class, and it initializes a list of id's, and it sets some defaults.
So it is basically extending it with file name constructor from the parent class. getVersion is just over written. This getMaxItemsPerFeed, that's reading a preference pane value, we'll get to that later on in the course. This addNewIndex, there was a previous version of this App, which used a database file that did not have an index, and so it just creates the index if it doesn't exist. You'll notice that it is just using SQL for that, and it's calling doQuery on the parent class.
RSSDB because it inherits BWDB, everything in BWDB is available. As a result of that, all of these methods that have to do specifically with feeds and with items, these are all very, very simple and straightforward some of them have some SQL in them but they are just calling doQuery from the parent class. So it makes it really easy because we already have this wrapper, it makes it really easy to write these functions that are very specific for this application and to simply put them in a class extension.
Some of the most complicated stuff like this deleteOldItems is all accomplished in SQL. So you can see that it makes this job a lot easier that we can just extend a class that's already written, that's already wrapping the SQLite C interface in a nice Objective-C interface. Now, we can come out here to our TestbedViewController. Instead of BWDB here, we are going to import RSSDB.h.
Now, we are going to come down here in our dispRow and our Test database. We are just going to replace those. So I'm going to select all of that, and I'm going to come back out here to the Finder, and in Chapter 02, RSSDB-testbed-code.txt, I'm just going to select all of that and copy it and come back over here into Xcode and paste that in. You can see that's very simple, so I am pressing Command+S to save. You see all this does is it initializes this RSSDB object.
It passes it the file name bwrss.db, that's the database that we created in the last lesson, and we've already copied it into our project. It just displays the RSSDB version, and it reads these feed id's and displays them. Now we are going to make sure that the iPhone Simulator is selected and go ahead and run this in the Simulator. There we have our results. It is possible that you may get just the version number and not the actual database.
There may be some errors down here on the screen. If that's the case, what you need to do is you need to actually delete the App from the Simulator and run it again. The reason for this is if this App has been loaded before, and it did not have the database in it, and then you load the database in Xcode and run it in the Simulator, it may not actually replace the whole bundle in the Simulator. So you have to delete the bundle and load it again. It sounds complicated, but if you just follow these steps, it should work.
The first thing I am going to do is I am going to select Xcode, press Command+Dot, and that will bring up this message Finished running Testbed on the Simulator. Let's switch back to the Simulator, I am going to click on Testbed here and hold it down, you notice that it does this little wobbly thing just like on your iPhone when you go to delete something. I am going to press the Delete, and I am going to actually delete that whole bundle. Because my Simulator is the small version because I have the small screen, there is no button, I happen to know that Command+Shift+H presses the Home button.
You see that the whole second page is gone now, because there is no App that goes there. You may have other Apps there, and that's fine. You just want to make sure deleting this one. Now I come back over here to Xcode. I have deleted the bundle off of the Simulator, and when I run it again, it is going to load up the correct bundle. You see that now we have the database. If you get that condition where it just shows the RSSDB version number, and it doesn't show any of the data that is probably because you've got an old version of the bundle on your Simulator that didn't have the database in it. You just delete that bundle, run it again from Xcode, and it should load up the correct bundle with the database in it.
Here, we are just displaying these rows from the database, and we're showing that our App Specific Interface is working. As we go through the rest of the application the rest of this course, we will be using and discussing more of the methods from this library. An App Specific Database Interface is a simple technique, and it is a powerful tool. As your apps get larger, this technique becomes more and more valuable. I find that it is almost always worth the effort.
Get unlimited access to all courses for just $25/month.Become a member