When you work as a developer everything seems challenging at the beginning whether it is the UI, logic, functionality, or storing the precious data that our user is going to use in day to day life. In this article, I will explain to you, why Persisting data is so important in iOS?, and the simplest way for storing the small bits of data using User-Defaults.
What is Data Persistence?
Understanding the meaning of persistence is important as an app developer. The importance of the storing data in most of the modern applications is a challenge, and when achieved then, it provides a better and interactive user experience. Suppose, you were using an app to fill a form, then suddenly you had an incoming call and since calls are the unavoidable interrupts for phones, your app will go to the background and you have to receive the call. When you returned back, you see that you had lost all the data that you put inside the form. This is the problem that we need to solve using data persistence.
Persistence is “the continuance of an effect after its cause is removed”. In the context of storing data in a computer system or mobiles phones, this means that the data must survive after the quitting of our application. In other words, for a data store to be considered persistent, it must write to non-volatile storage.
Why Do We Need to Store Data Locally?
Well, as I stated earlier, interrupts like call or another thing may hamper your important tasks in between on any app. Now, it would be really annoying when your work gets lost and all data is gone so, saving them locally on the device’s memory is very important.
Talking about IOS, there is a life cycle of the app from launching to terminating, and between these, a user performs his/her task. When you press the home button, the apps go in the background i.e. the app you were working on is no longer visible. You can think of them like a deck of cards where one card is placed over another. The cards are the app. When your app gets terminated then you lose data in normal apps that you made until you use the data persistence mechanism on IOS for storing data locally on the users’ device.
The app in the background may be processing out its tasks or may be accessing the memory of your iPhone. The cases where the resources get reclaimed from your app then the app is terminated rather than running in the background. The life cycle of the IOS app is shown below.
The problem occurs when the app terminates and our data is not stored locally. The terminating of the app can occur in the following scene:
- The operating system is trying to reclaim the resources because the user is trying to use various apps and the memory space just runs out.
- If the user force quit the app for any reasons or by mistake.
- Installing updates can really end up terminating your app.
- If the operating system is updated.
This is one of the ways to persist the data locally. On IOS, The ways we can persist data locally are:
- UserDefaults: for small bits of data
- Coadables: saving items inside the documents.
- Coredata: SQL lite backend database basically we create a relational database.
The UserDefaults class written by Apple provides us a programmatic interface for interacting with the defaults system. The defaults system allows an app to customize its behavior to match a user’s preferences. For example, you can allow users to specify their preferred units of measurement or media playback speed. Apps store these preferences by assigning values to a set of parameters in a user’s defaults database. The parameters are referred to as defaults because they’re commonly used to determine an app’s default state at startup or the way it acts by default. For more information read the Apple documentation.
Let us see through an example of what can we do using UserDefaults while Coding using Swift language.
we can set an URL, key-value pair, bools, doubles, floats, integers. Suppose you write the code as:
defaults.set(value: Float, forKey: String) and set the float value as 3 for a music player app by giving key as “myVolume” when you do so than later, on you can always use the persisted data to always set the volume of that particular key value.
There could be more implementation as shown below:
Here, the any? the data type is grabbed by a .object method as shown above. You can also save collections using UserDefaults.
you can see the array value is retrieved using the UserDefaults. The grey box even suggest that the default key name is a string and is a kind of array and the return type is actually an optional array i.e. of type [any]? and you can even downcast them in any type by using as! [datatype]
Similarly, you can do these things for dictionaries as well.
Note: The spelling of the key is of a great deal and since it is hard coded so, you must write them as they are specified while working on the big projects. A suggestion or a tip would be declaring a separate constant for them could reduce the typing error. For Example:
let myDictionarykey = “data”
When the amount of data is huge, they become unmanageable and the reason is, UserDefaults is not a database and it should be used as a database. Since all the value stored by the UserDefaults is saved into the .plist file and it actually loads us simultaneously with the app, and saving all the data in them can really slow up the loading to a great extent, and if you are looking for a small piece of data from that file, even for grabbing that data, the device or your simulator has to load up the entire file unnecessarily. Thus, the efficiency of our app is tremendously affected when it is used for huge data and as a pure database.