Core Data Gotchas

Here is a list of the problems I encountered working with my first Core Data App. Maybe it will help others from making the same pitfalls.

Cells not Displaying Content, tableView: cellForRowAtIndexPath: Not being called...

So I had no data displaying in my Table View Controller, I put loads of breakpoints all over the place trying to find it. My problem was I didn't understand the Application life cycle well enough. I had previously placed a programming stub in tableView: numberOfRowsInSection: that returned 0! And so the cellForRowAtIndexPath was never called. This was a rookie mistake that cost me some time. 

Using Prepopulated Data with Core Data

All of Apple's example code for core data gives you a solution without prepopulated tables. I take that back, Core Books does. But after reading all of the documentation on Core Data and hand writing the Location example along with the Core Data with the iPhone Tutorial I still did not know how to use a prepopulated Table, especially one with my own design! 

A few google searches and a lot of time reading lead me to this Blog. Basically make a Datastore if there isn't one in your resources folder. Copy past that store from a very obscure file directory (/Users/<Username>/Library/Application Support/iPhone Simulator/User/Application/<Application GUID>/Documents/<database name.sqlite>) and place that database into your App.

I'll be spending a lot of time testing this method because the end result that I want is a lot like Apple's Books example. Another great example which I wish I had found earlier is the Recipes App given to developers by Apple. Both have a wealth of Information contained in their code. The Recipes App is the only app I have seen that has a UITabBar and UINavigationBar in the same Interface Builder .xib file and functions properly. Apple's documentation in this has been lacking and many developers over the last year have asked how do you get navigation controllers working under tab bar controllers? Apple's Recipes app has your answer.

Search

Search isn't made easy on the iPhone. It is for the end user, but us developers need to use a lot of Apple's code to get it working. At first I wanted to use The Three20 framework to make search easy. After a while I realized that Apple has this better documented than the guys over at Facebook. The BusApp doesn't use Three20 now and I'm happier for it. It'll be easier for other developers to understand, and the binary will be much smaller. 

One gotcha with search is the placement of the search bar. At first I placed the search bar at the top of the table view. This method covers up the topmost item of the table view, not cool but easily fixed, just offset the data in the table by one and you've got them all displayed. I hope I can track down another solution for this problem before I publish because this seems a little cheap at the moment.

Editing Your Favorites

I got favorites up and running really easily, but I forgot a feature when I was programming it. Users may want to unfavorite a bus! Once I decided that the bus app would be incomplete without this feature I started refactoring my code to include editing. This only added 200 lines of code, to my Favorites view controller. 

Objective-C and String Comparison

 

if ( [aNode localName] ==@"h1") {}   is not the same as :  

if([[aNode localName] isEqualToString:@"h1"]){}

Another rookie mistake i made because I'm coming from Javascript

 

Conclusion

I learned that Core Data isn't hard. Its just time consuming. You need to get your resources ready. I read a few books before starting the Bus App to get myself back into an Objective-C. If you've done any type of work with databases before Core Data is going to be natural to you.