Data Persistence in IOS

ways to store data in IOS world

Imran Jutt
4 min readJun 17, 2017

The first thing you would ask yourself what does data persistence mean? If we look at the definition of Data Persistence its keeping the data you are working with around. In computer science, persistence refers to the characteristic of state that outlives the process that created it. This is achieved in practice by storing the state as data in computer data storage.

When it comes to data persistence in IOS, there are six major ways we can persist data in IOS. Following are the data storage options in IOS.

UserDefaults

UserDefaults are a great way to save a small amount of data.It’s not intended to save a lot of user generated data though. For that use case there are other possibilities on iOS. Instances of the following classes can be saved using UserDefaults

  • NSData
  • NSString
  • NSNumber
  • NSData
  • NSArray
  • NSDictionary

Arrays and Dictionaries can only be saved in userDefaults if they contain values of one of the above mentioned types.

Similarly simpler types like Bools and Integers can also be saved in the userDefaults. You might ask how exactly these values are stored internally? Well internally a plist file is maintained by the OS for each application, all the values saved in userDefaults are maintained in that plist.

UserDefault Limitations: The maximum data userDefault can hold is the maximum file size that IOS allows us. Typically it is around 4 GB but it may change in future OS releases. The important thing to point out here is that the entire file is read in and written out as a whole, so if we are using UserDefaults to store a large amount of data that only changes in parts, you will be wasting a lot of time doing unnecessary I/O .For these reasons it is best to use UserDefaults for storing and retrieving small pieces of data.

Data Archiving and Unarchiving: We can also store our custom objects in userDefaults. We achieve this by conforming our class to NSCoding protocol. We can then convert our custom object into NSData with the help of NSKeyArchiver class. The NSData is then stored into userDefaults like other objects. Similarly we can get NSData from userDefaults and then using NSKeyUnarchiver convert the NSData back to our custom objects. Its time for some code ;)

Property List

Property lists are another great way to store our data. However like userDefaults it is not intended to save large amount of data. To save data into a property list we simply call writeToFile method of Array or Dictionary. It will save the changes into a plist file.

SQLite

If your application deals with large amount of data with relationships then you may want to look into SQLite. SQLite is lightweight embedded relational database. It is serverless, zero-configuration, transactional SQL database engine. In other words it is embedded in the application and therefore very fast. SQLite is written in C so all its apis are in C Language. In order to bridge the gap between SQLite and Objective C, a number of Object Relational Mapping (ORM) have been created over the time. Following are the two most widely used ORM’s that you can take advantage of while building IOS apps.

  • Realm
  • FMDB

Keychain

If you want to save highly sensitive and secure data like passwords and secret codes then there is a good news for you. In IOS we have keychain for this specific purpose. The API is a little bit low level, but Apple has created a very good wrapper for using the keychain. It’s an Objective-C class, so you have to insert it in the bridging-header file. Furthermore, it’s not ARC code. So in order to use it, you have to disable ARC for these source files.

Saving Files

Of course you can also directly save all types of files to the file system. However, you can just access the file system within the app container due to security reasons. Basically there are three folders within the app container:

  • Documents: This is the perfect place to save user generated content. It’s also backed up to the iCloud automatically.
  • Library: In this folder you can put files that are not generated by the user and that should persist between launches of the app. However, some files could be deleted when the device doesn’t has enough free space. There are some subfolders within the library folder for different purposes. For more details take a look at the iOS file systems basics document.
  • tmp: In this folder you can save files that the app just needs temporarily. They can be deleted by the operation system when the app is not running.

CoreData

Core Data, Apple’s solution for persistence, allows applications to persist data of any form and retrieve it.Core Data isn’t technically a database, although it usually stores its data in one (an SQLite database, to be precise). It’s not an object-relational mapper (ORM), though it can feel like one. It’s truly an object graph, allowing you to create, store, and retrieve objects that have attributes and relationships to other objects. Its simplicity and power allow you to persist data of any form, from basic data models to complex.

This sounds very straightforward, but at the beginning Core Data is very complex to use though. But it’s worth the learning effort because Core Data provides you a lot of possibilities. For example, you can perform undo and redo operations. It’s also very performant and Core Data lazy loads the data.

I have tried to cover all of the possible methods that are being used to persist data in IOS. Please feel free to contact me for any other information that you would like to see in this article.

--

--