Advertisement
iOS SDK

Beginning iOS Development: Data Persistence

by

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 here.

Further Information

Property Lists

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.

SQLite

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.

Seeding

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.

Core Data

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.

Related Posts
  • Code
    iOS SDK
    Securing and Encrypting Data on iOSPs8e2e preview image@2x
    Whether you're creating a mobile application or a web service, keeping sensitive data secure is important and security has become an essential aspect of every software product. In this tutorial, I will show you how to safely store user credentials using the application's keychain and we'll take a look at encrypting and decrypting user data using a third party library.Read More…
  • Code
    iOS SDK
    Blocks and Table View Cells on iOS94dp7 preview image@2x
    A table view cell doesn't know about the table view it belongs to and that's fine. In fact, that's how it should be. However, people who are new to this concept are often confused by it. For example, if the user taps a button in a table view cell, how do you obtain the index path of the cell so you can fetch the corresponding model? In this tutorial, I'll show you how not to do this, how it's usually done, and how to do this with style and elegance.Read More…
  • Code
    iOS SDK
    Working with NSURLSession: Part 3E548b preview image@2x
    In the previous tutorials, we explored the fundamentals of the NSURLSession API. There is one other feature of the NSURLSession API that we haven't look into yet, that is, out-of-process uploads and downloads. In the next two tutorials, I will show you how to create a very simple podcast client that enables background downloads.Read More…
  • Code
    iOS SDK
    Objective-C Succinctly: Exceptions and Errors0e5ds8 preview image@2x
    In Objective-C, there are two types of errors that can occur while a program is running. Unexpected errors are "serious" programming errors that typically cause your program to exit prematurely. These are called exceptions, since they represent an exceptional condition in your program. On the other hand, expected errors occur naturally in the course of a program's execution and can be used to determine the success of an operation. These are referred to as errors.Read More…
  • Code
    iOS SDK
    Objective-C Succinctly: Data Types0e5ds8 preview image@2x
    Objective-C has two categories of data types. First, remember that Objective-C is a superset of C, so you have access to all of the native C data types like char, int, float, etc. Objective-C also defines a few of its own low-level types, including a Boolean type. Let's call all of these "primitive data types."Read More…
  • Code
    iOS SDK
    Data Persistence and Sandboxing on iOSPreview image@2x
    Persisting data across application launches is a requirement that most iOS applications have, from storing user preferences using the user defaults system to managing large datasets in a relational database. In this article, we'll explore the most common strategies used for storing data in an iOS application. I will also talk about the file system on iOS and how application sandboxing affects data persistence.Read More…