Caching on Swift Using NSKeyedArchiver

khalis murfid
Inside PPL B7
Published in
4 min readMay 11, 2020
Photo by Jessy Smith on Unsplash

From https://whatis.techtarget.com/definition/caching,

A cache is a temporary storage area. For example, the files you automatically request by looking at a Web page are stored on your hard disk in a cache subdirectory under the directory for your browser. When you return to a page you’ve recently looked at, the browser can get those files from the cache rather than the original server, saving you time and saving the network the burden of additional traffic.

In my language, a caching is basically what makes your twitter feed still intact even when your phone didn't have any access to the internet. Caching saves a lot of time and data. Your Twitter app didn’t have to fetch the data of your friends' tweet again from the server.

In this article, I want to give you guys an insight about how to do a simple caching app in swift.

See that Car list? Let’s assume that that car is called using some API call from fetchData() function.

Every time that that page is loaded, it will always call fetchData(). The truth is we don’t need to always call that function. We can use caching to make our car data persistent. So even after we kill our app, the data is still there, hence the name ‘persistent data’.

Suppose we have this Car model class.

This is the simplest form of model. Your model may be vary.

The first thing we got to do is change our Car model from that to:

We need to make our model conform to NSObject and NSCoding, so we need to add encode and required init?(coder:NSCoder) function into our model. This step is important because to save our data to storage, there will be an encode and decode process. Don’t worry, swift already handle the encode and decode if you inherit your class to NSCoding.

And now for our holy trinity for today, we need to add this 3 function

to our ViewModel/ViewController. the function saveData() will save our list of Car ( you could see it on the parameter) and save it to a certain path. After that, we can always get the file using getObject() method.

Every time our fetchData() is called, that’s when the API call is made, we need to put the result to our storage using saveData(). So, our fetchData() will look like this :

We save the data to the current path with the name file “Car.archive”. So it’s safe to say that our cache is the newest data we get from the API call. The next thing we need to do is we need to make sure that every time our page is load, we put the cache as the data sources. This behaviour can be achieved using the following tweak on ViewController :

Now our dataSources will be filled with our cached data that we save on “Car.archive” file. And

That’s it, guys. That’s how to use NSKeyedArchiver as an caching for data sources. That’s how easy it is. I hope you guys enjoy my short article. Actually, there are so many ways to cache data in swift. I really encourage you guys to find other references for caching or persistent data in swift.

Thank you and keep curious !!

--

--