This website uses cookies to ensure you get the best possible experience. See our Cookies Policy.
Storing Data On Mobile Platforms: iPhone
In almost every mobile application you will have need to store data, whether it be to preserve state, to save information for the user, or to cache data received from a web service for “offline mode”, it is almost inevitable. Depending on your applications specific needs, there are many different options you can choose from, when it comes to storing data on the iPhone.
First on the list is NSUserDefaults. The NSUserDefaults class is actually based on a form of data storage called a plist, which we’ll cover in a moment, and is a great way to store simple data. For instance, you could save whether a user wants sound effects in the app, and the object will be saved in what is known as the iOS “defaults system”. The iOS defaults system is accessible all throughout the code of your app, and any data saved to it will persist through all application sessions.
Being based on plists NSUserDefaults is limited to storing only objects from these classes: NSData, NSString, NSNumber, NSDate, NSArray, and NSDictionary. As such, NSUserDefaults is not well suited to storing complex model objects, or large sets of data. It is best used as a means to store lightweight app state or user settings.
Saving data to NSUserDefaults
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:@”Text to be saved” forKey:@”NSStringKey”];
[prefs setInteger:101 forKey:@”NSIntegerKey”]; [prefs setFloat:9.87 forKey:@”floatKey”];
[prefs synchronize];
Retrieving data from NSUserDefaults
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSString *storedString = [prefs stringForKey:@”NSStringKey”];
NSInteger storedNSInteger = [prefs integerForKey:@”NSIntegerKey”]; float storedFloat = [prefs floatForKey:@”floatKey”];
If you’ve done very much development in Xcode, I can guarantee that you’ve worked with at least one plist. Granted, you didn’t have to write the code to actually access it’s data, you’ve certainly worked with the info.plist file in an Xcode project.
A plist is actually just an xml file, which, same as any xml file, you can use to store data. Since it doesn’t have the wrapper of the NSUserDefaults class, using a custom plist is actually faster than using NSUserDefaults. Though, it is still restricted in the types of data it can hold, and a plist is not really suitable for storing complex data objects.
In order to use a plist, you first have to create a new .plist file in your project. Then, before attempting to save or retrieve data from your custom plist you’ll want to execute this:
NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *path; path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path]) {
NSString *bundle; bundle = [[NSBundle mainBundle] pathForResource:@”data” ofType:@”plist”]; [fileManager copyItemAtPath:bundle toPath: path error:&error]; }
You have to do this, before anything else, because the original file, created in your project, is stored in the bundle directory. The bundle directory, is a read-only directory, so you can’t actually save data into the original plist you created in the bundle directory. In order to save to it, you have to create a copy of the .plist file in the document directory, where iOS permissions will allow you to read and write on the file.
Now, writing data to a Plist
NSMutableDictionary *data; data = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSString *toStoreString = @”Text to be saved”;
[data setObject:toStoreString forKey:@”NSStringKey”]; [data setObject:[NSNumber numberWithInt:101] forKey:@”NSIntegerKey”]; [data setObject:[NSNumber numberWithFloat:9.87] forKey:@”floatKey”];
[data writeToFile:path atomically:YES];
[data release];
Reading data from a Plist
NSMutableDictionary *savedStock; savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
NSString *storedString;
NSInteger storedNSInteger; float storedFloat; storedString = (NSString *)[savedStock objectForKey:@”NSStringKey”]; storedNSInteger = [[savedStock objectForKey:@”NSIntegerKey”] intValue]; storedFloat = [[savedStock objectForKey:@”floatKey”] floatValue];
[savedStock release];
SQLite is a relational database management system contained in a small C library that is implemented as a self-contained, serverless, zero-configuration, transactional SQL database engine. In contrast to most other database systems, SQLite is not a separate process that is accessed from the application, but is actually integrated into it. SQLite is a popular choice as an embedded database for data storage in applications, and is arguably the most widely deployed database engine. Also, all of the source code for SQLite is in the public domain.
When storing complex model objects, SQLite is far better suited to managing that data than a Plist, though, setting up an SQLite database for storing your model data is quite a lot more complex than setting up a Plist. If you want to learn how to access a SQLite database in Objective-C, you can read this iPhone SDK Tutorial: Reading Data from a SQLite Database.
Core Data is an Apple framework described as a “schema-driven object graph management and persistence framework.” The framework manages where data is stored, how it is stored, data caching, and memory management. It was initially written for Mac OS X and was ported to the iPhone for the release of iOS 3.0.
Core Data allows you to create and use a relational database, perform record validation, and perform queries without using SQL conditions. It essentially allows you to interact with SQLite in Objective-C and not have to worry about connections or managing the database schema. The main plus of using Core Data is that it eliminates the need to develop and write complex SQL queries or manually handle the SQL and output of those operations.
Again, using Core Data is a far more complex process than using a Plist, if you want to learn how, you can use this Core Data Tutorial for iOS: Introduction
This is not meant to be an explanation of the very best option for data storage in iOS, and why it is the absolute best. Every option has its own set of advantages, as well as its own issues. What option you choose has to be entirely based upon your own application’s specific needs.
Stay in touch
Subscribe to our newsletter
Post Image by GDS Infographics
Posted by: PMG Advertising Agency