Improve App Caching with Codable

iOS Mar 20, 2023

Efficiently managing an app's data and avoiding needless work is crucial to creating a fast and responsive user experience. This is more than just improving the UI and execution speed. Inefficiency may occur when the same set of data need to be reloaded every time it appears on the screen. By selectively caching values and objects, we can greatly improve an app's performance overall.

Here at Halodoc our goal is to continuously improve performance in order to make our user's experience better. Thus recently, one of our key objective is to migrate our implementation of NSCoding into Codable. This objective is set to increase our app's performance, reduce unnecessary waiting time, and clean our model from dual implementation of NSCoding and Codable.

In this blog we will discuss about what is caching, types of caching, what is persistent cache, how we are implementing it using NSCoding and how to migrate it into Codable.

What is Caching?

Caching is a process of temporarily storing data which app can quickly access and use, thereby reducing the response time of data retrieval, minimising network traffic, and reducing the app's dependence on external sources. Caching can be used for various types of data, including images, text, and other types of media.

Types of Cache

Memory cache
Memory cache is a caching mechanism that temporarily stores frequently accessed data in a faster access memory, such as RAM, to reduce the time it takes to retrieve the data. The most commonly used memory caching mechanism is the NSCache class, which is part of the Foundation framework.

Persistent cache
Persistent cache is a caching mechanism that allow the data to persist even when the application is closed or the device is restarted. Persistent cache stores data in a more permanent storage medium, such as the device's hard drive or flash memory, making it possible for the data to be retrieved when the application is launched again.

When to use persistent cache?
There are many different usecase where persistent cache can be used, one of the example will be to store history related data. Since history data will always be the same (until new data present), we can reduce network call for accessing this data by simply store it in a persistent cache. Later when the new data is present then only this cache need to be updated.

How to use persistent cache?
The idea of persistent cache is to keep our data in a semi permanent storage, thus we will use FileManager as our storage manager. To store our data inside file directory we can use this function

And to get the data back we can access the same directory again and convert the data into our model

Since NSKeyedArchiver is a concrete subclass of NSCoder, thus our DataModel class need to conform NSCoding protocol

Problem with NSCoding is, it can be slow for large objects, this is because NSCoding serializes objects to a file. Thus working on multiple class that conforms to NSCoding will affect our development time overall, since loading such class will take longer time than any other class.

But what can we do?
NSKeyedArchiver need the data to be using NSCoding for it to work, but doing so will make our development slower.

Introducing Codable

With the introduction of Swift 4.0, Apple also introduced Codable protocol. Codable is actually a type alias that combines two protocols, Encodable and Decodable into one. With this new protocol the need to use NSKeyedArchiver in Swift has diminished significantly. Instead of using NSKeyedArchiver, now we can use JSONEncoder or PropertyListEncoder for serializing custom objects to save them into disk.

But don't worry if we still want to use NSKeyedArchiver, Swift team also introduced two new methods for NSKeyedArchiver to make it compatible with Codable.

Moving to Codable

With the introduction of encodeEncodable and decodeDecodable method, we can remove our NSCoding implementation and change it to Codable. First let's change our previous model

Now that our model is not using NSCoding anymore, we need to update our save cache method into using encodeEncodable

Same goes for our get cache method, we will use decodeDecodable method

And that's it!
We have done migrating NSCoding into Codable.

Conclusion

Caching is an important part of application development, it allows us to reduce data retrieval, minimizing our network traffic, and reducing our app's dependence on external sources. With the introduction of Swift 4.0 Apple opens up a new door for us, the migration that we did in this blog is possible with the new protocol Codable that Swift introduce. With this we are able to remove the old implementation of NSCoding and it's downside!

If you are interested in learning more about development, don't forget to check other blog that we have, and thank you for reading this one.

Join us

Scalability, reliability, and maintainability are the three pillars that govern what we build at Halodoc Tech. We are actively looking for engineers at all levels and if solving hard problems with challenging requirements is your forte, please reach out to us with your resumé at careers.india@halodoc.com.

About Halodoc

Halodoc is the number 1 all-around Healthcare application in Indonesia. Our mission is to simplify and bring quality healthcare across Indonesia, from Sabang to Merauke. We connect 20,000+ doctors with patients in need through our Tele-consultation service. We partner with 3500+ pharmacies in 100+ cities to bring medicine to your doorstep. We've also partnered with Indonesia's largest lab provider to provide lab home services, and to top it off we have recently launched a premium appointment service that partners with 500+ hospitals that allow patients to book a doctor appointment inside our application. We are extremely fortunate to be trusted by our investors, such as the Bill & Melinda Gates Foundation, Singtel, UOB Ventures, Allianz, GoJek, Astra, Temasek, and many more. We recently closed our Series C round and In total have raised around USD$180 million for our mission. Our team works tirelessly to make sure that we create the best healthcare solution personalised for all of our patient's needs, and are continuously on a path to simplifying healthcare for Indonesia.

Kevin Josal

Software Development Engineer iOS