I just want to read and write data in my iPhone app.
You can’t do that without making a fateful choice. There are three major options: Property Lists, SQLite, and Core Data. Core Data is Apple’s preferred method but it doesn’t work on non-OS 3.0 devices. This means a lot of iPod Touch users (who have to pay to upgrade) will miss out on your application and you’ll miss out on their money. The following is a brief comparison of the three options based on my limited understanding.
Read data from a plist file, turn the data into objects and display the object data in your UI. Set it up so that when objects are modified, the plist is also modified.
- Simple to understand.
- Easy to work with.
- Cannot run complex queries on them (at least not easily).
- You have to read the entire file into memory to get any data out of it and save the entire file to modify anything in it.
- The best tutorial I could find is in chapter 11 of Jeff Lamarche’s excellent Beginning iPhone 3 Development. Don’t be cheap, buy the book.
- Watch the first part of this Stanford lecture or look through the slides.
- Note: save yourself a lot of trouble and learn how to archive and unarchive your data objects rather than using the raw plist in your interface. You can learn how to do this with either of the above resources.
Bottom line: If you’re just starting out and your app doesn’t use a ton of data, plists are probably the way to go.
Set up the SQLite database (tables and that kinda thing), set up the database connection, query the database, turn those queries into objects in your application, display object data in the UI, do some funky stuff to get object data saved back to the database.
- Unlike plists, you don’t have to load the whole database up front. This makes SQLite more suitable for applications with lots of data.
- Better than plists for slicing and dicing data.
- Steeper learning curve than plists.
- Can get tedious to work with.
- iPhone SQLite basics (tutorial)
- Collection of SQLite tutorials
- Watch the second part of this Stanford lecture or look through the slides.
- How to make a todo app with SQLite
Bottom Line: Use if you want to build an iPhone 2.x compatible application with lots of data.
Its new, its exciting, and its probably what most developers will use from here on out.
I have not spent enough time with CoreData to summarize it; check out the tutorials (below) to learn more about it.
- Nearly all the benefits of SQLite with a lot less hassle (Apple does a lot of the dirty work for you).
- As Apple’s preferred method it has a lot more official documentation and sample code (it seems the articles and sample code for the other two methods have mysteriously disappeared from Apple’s website).
- Probably more pros, comments please.
- Steeper learning curve than plists.
- Killer: only works on iPhone OS >3.0. Its killer if your market consists largely of iPod Touch users (who have to pay for upgrades).
Core Data Tutorials
- The definitive Core Data tutorial
- Collection of tutorials
- Video Tutorial
- Core Data Recipes: Sample Code
- Core Data Books: simple Core Data application
Bottom line: use if you are making an iPhone 3.0 only app.
And the Winner is…
I have messed and dabbled and toiled and troubled with this question for months. At first, I tried SQLite because it seemed to be the way to go at the time. It was great but it seemed overkill for my simple application and managing all that extra code was hassling me to death.
Then came the new guy that everybody was talking about: Core Data. It was everything a programmer could dream about for reasons I couldn’t possibly comprehend at the time. It was the new way to do things and the best thing since sliced arrays, so I tried it.
Everything was going great and I was definitely seeing some benefits over SQLite but it was then when my dreams were quickly shattered by a simple fact: Core Data only works on iPhone 3.0. Ahh, you say, but the adoption rate of iPhone 3.0 is off the charts. Ahh, I say, but you forget that my target market is college students, most of whom:
- Own iPod Touches because they can’t even afford deodorant, let alone an iPhone.
- Haven’t upgraded to iPhone 3.0 because they don’t want to pay ten colossal bucks.
I am a college student and I am among these creatures every day. Believe me, its true.
Bottom line: I ended up choosing property lists because (1) I didn’t need the power of SQLite and (2) I didn’t want to alienate my potential iPod Touch buyers who have not yet upgraded to OS 3.0.Tweet