Storing your data
After finishing the first steps we have a working GUI with custom classes and a model in the background, its time to store the model data so we don't have to re-add it after we restart the application.
Before we look at how we can accomplish that, we have to think about the necessity of automatic backups. So the question is where do we store the data:
- In temporary directories?
- In directories iTunes creates backups for on synchronization?
First of all, you only have access to the application directory on the iPhone, anything else is protected. That means you are restricted to the directories automatically provided for your application.
There are two directories for temporary storage “/Libraries/Cache” and “/tmp”. These directories will be cleaned by the iPhone OS automatically. You should only store information which is not crucial for your application at this places and which can be reconstructed easily.
The “/Documents” and “/Libraries/Preferences” directories offer an place for permanent storage. iTunes will backup these directories on
A helpful way is to divide your data in to live and static data:
Live data is the information you get through user interaction and which needs to be stored permanently. You should distill this data, so every information that can be reconstructed from a base set of data shouldn't be stored.
Static data is stuff like images, sounds, text,... This information is already known while building the application and is not subject to change during the application lifetime. Separate it from your frequently changing data.
After thinking about these basic data related questions it's time to clarify how we can store it?
There are two ways by using functions/classes of the framework:
They both follow the same principle, generating a NSData object from your data objects which can be used for storing and retrieving your data.
The first way is via an PropertyList (NSPropertyListSerialization)
If you are just using the base build in types (NSString, NSArray,...) you can use this way to manage your data in a file. It is as straight forward as it sounds.
The second way is via an Archiver (NSKeyedArchiver)
If your are using custom classes you can not use the PropertyList approach. You have to use an Archiver. To use this class your custom classes must be designed according to the NSCoding protocol, meaning you have to provide the methods “encodeWithCoder” and “initWithCoder”. In these methods you can store your data using the functions of the given NSCoder object. There are a few other archivers too, but if you get the point, its straight forward.
Or one way by using SQLite in your application
Using SQLite is quite simple. To use a database as your data storage you only need a few base objects and functions from the SQLite API. I recommend the appropriate documentation from www.sqlite.org to understand the basic concepts.
One thing you can consider is building your database on the fly or preparing it upfront. If you build it ad hoc you have to include the whole logic to create your tables in the application, which is in the majority of cases not necessary. – There are cases which make it necessary to do it on the fly, but I don't think they are appropriate on this platform right now – You can create the database on your mac and insert the appropriate tables. Then you add the database file to your XCode project resources and the file will be automatically deployed to the “/Documents” directory.
Now you have 3 basic ways to manage your data on the iPhone!
P.S: There are some more ways to store data on the iPhone, preferences and resource bundles. But if you are familiar with these 3 ways, preferences and resource bundles are just tools for some special cases (Data which is live and static, and big static data).


