Local Data Storage in iOS

Local Data persistence is one of the key features of an any mobile app. There are times when we need to load some data when there is no internet connection. There are also times when we need to make an API call to fetch the same data again and again. In these cases it is better to store data locally. Persisting the data locally helps in many ways.

  • We can use the persisted data to reduce loading time by caching data.
  • There is no need to make an API call every time to fetch the same data again and again. This will help in the performance improvement of an app 
  • Persisting data locally helps the app to work in offline.

In this post I will try to shed some information on the most used ways of storing the data locally in iOS. Below are the some of the ways.

  • KeyChain
  • User Defaults
  • Core data

1. Keychain

Keychain is used to save some sensitive or private information like username, password, api keys, tokens etc in an encrypted format. If the data to be stored doesn’t fall under the above category, then generally people use other ways of storing data like User Defaults or Core Data which we will cover in the next section. 

There are some external libraries like KeychainAccess, Valet etc that can be integrated in the project. These libraries provide the external wrapper over the Apple Keychain API’s and makes using Keychain APIs extremely easy.

2. User Defaults

In most of the cases, we prefer using User Defaults to persist small amount of data locally. This is the most easiest ways of persisting the data amongst others described in this post. This works as a Key-Value pair where key is a string and value can be only property list objects like –  NSDataNSStringNSNumberNSDateNSArray, or NSDictionary.

Reference – https://developer.apple.com/documentation/foundation/userdefaults/1414067-set

  • Set the Value: While storing the value, it provides the method set(_ value: Any?, forKey: String).
    The value here should be any of the types specified above. 
  • Get the value: It provides the methods to fetch data of specific types like bool(forKey: String), int(forKey: String), string(forKey: String), etc. However it also supports another method object(forKey: String) to retrieve Any

Small pieces of data like user settings, device settings, preferences settings can be stored in User Defaults. 

Note: We cannot store custom objects like classes or structs directly in User Defaults. To store the custom objects, we need to convert those to data objects first and then store it as data.

3. Core Data

Core Data is an object graph and persistence framework provided by Apple in the macOS and iOS operating systems. (https://en.wikipedia.org/wiki/Core_Data).
While creating the project, we get an option to enable Core Data in the project. If it is enabled, everything will be wired up already for you. In case core data is not not enabled, one has to set up manually.

Core data is used to store complex objects like classes and structs with different attributes.

In Core Data, Data models are represented in the form of Entities and its properties are represented as Attributes. Core data also provides different relationships between the entities.

Core Data by itself is a very big topic and can follow few links given below.

That being said, the above methods are the not the only ways to store data locally. Most of the times, these are used more frequently in day to day programming. There are also other ways to persist data locally like File System and SQlite. 

FileSystem is used to cache the documents, images that we download from internet. If the same images/documents needs to be downloaded more frequently, then one can cache these documents and read from cache first before making an API call to download the same documents again. 

FileSystem can also be used to store some settings as key-value pairs in a plist files.

Leave a Comment

Your email address will not be published. Required fields are marked *