This Cyber Monday Envato Tuts+ courses will be reduced to just $3. Don't miss out.
In the seventh installment of the Beginning iOS Development screencast series, we are looking at Data Persistence in iOS Applications. The screencast includes a short keynote overview of the different storage methods that you can use in an iOS application as well as the pros and cons of each method. After the keynote, the four implemented data storage methods are explained in detail.
As mentioned in the screencast, this demo application implements all four data storage methods. If you wish to implement one of them into your own application use the demo application given as a guide only.
Trouble watching the video above? Download the full quality version in MOV format .
Property List documents are an archived tree of objects. The Property List contains a root element which is either an NSDictionary or an NSArray. Inside the root element is the archived data. The classes that can be archived into a Property List are: NSString, NSNumber, NSData, NSDate, NSArray and NSDictionary. Providing any other objects to be archived as a property list will not write the file. A common mistake is to give a Boolean or Integer object to one of the items in the NSArray or NSDictionary. To store a Boolean or Integer use the NSNumber class.
As mentioned, using the SQLite C library can be tricky the first time around as it is very particular about the argument types given to functions. It's important to remember the differences when passing in strings and the arguments required for these functions.
If you want to bypass the C library there are a few Objective-C wrappers written for using SQLite on the iOS. FMDB is one particular wrapper that you can include into your Xcode project to avoid using the C library.
SQLite operates from a single file. When the application is launched for the first time you will have to run a file creation process to make the SQLite file available. In the screencast implementation a new blank SQLite database is created and SQL executed to setup the first table. However, you may want to have a database that has data already seeded into it. To do this you create a SQLite file with the table schema and required data inserted and then include the file into your Xcode project. In the database initialization process you can then copy the seed SQLite file from your Resources bundle to your application's Documents directory.
SQLite command reference
For more information on the functions available in the SQLite library go to the SQLite C Interface Functions List.
When implementing Core Data it is important to remember to create your Xcode project with the “Use Core Data for storage” option checked. That option will create some required properties and methods in your Application Delegate as well as include the Core Data framework into your project.
Core Data uses SQLite by default as the main database in your application. The Core Data framework in the iOS allows for 2 different database storage types and by default it is SQLite.
Getting started tutorials for Core Data
If you want a deeper guide on using Core Data for the first time then check out the First steps with Core Data article.
User Defaults (NSUserDefaults)
This method is different from the other 3 in the screencast. It is not used for storing collections of data as demonstrated in the other 3 implementations. Information stored in this method should be limited to user changeable settings and options.
Storing user authentication information
When storing usernames and passwords it is better to use the Keychain service available in the iOS. The Keychain is a secure storage medium to keep user authentication information on the device in a secure and encrypted manner. For more information on the Keychain go to the Keychain Services Programming Guide in the Apple Documentation.
Thanks for watching this screencast. If you have any further questions please drop a comment in below.