Part 10 — Storing data using Disk

In this tutorial, we will be learning about storing data using Disks.

Nitigya Kapoor
Hashtag by IECSE
5 min readMay 31, 2020

--

IECSE Crash Course: Development with Swift

This article is going to be the last article of the series IECSE Crash Course: Development with Swift. Through the course of 10 articles, we have made a small app which will display a set of users that we will fetch from the net. While making the app, we will cover some of the most important topics that every iOS developer must know. You can get the code till the previous article here.

For storing data will be using Disks, which provides powerful and simple file management.

To install Disks we need to use CocoaPods, that is a dependency manager for Swift and Objective-C Cocoa projects.

Setting up Disks

Open up your terminal and follow along with these commands.

Firstly, we need to install cocoapods.

Then, we need to do create a Podfile.

Open up the PodFile you just created, and inside we need to add the dependency for Disk.

After this, we need to install all the dependencies in your project.

Make sure to always open the Xcode workspace instead of the project file when building your project.

Disk

As Disks uses codeable protocol, we don’t need to worry about encoding or decoding, this just makes working with structs easier.

Disks follow iOS Data Storage Guidelines and allow us to save data in some predefined directories.

Documents Directory — .documents

We use this to store data that is user-generated and doesn’t change at all, data saved inside this will automatically backed-up by iCloud.

Caches Directory — .caches

Data that can be downloaded again or regenerated should be stored in this directory.

For example, in our app, we are fetching some data using a network call, so every time we open our app, it calls that request every time, we can cache this data to avoid this call again and again.

Application Support Directory — .applicationSupport

In general, this directory includes files that the app uses to run but that should remain hidden from the user. This directory can also include data files, configuration files, templates and modified versions of resources loaded from the app bundle.

Temporary Directory — .temporary

Data that is used only temporarily should be stored. Although these files are not backed up to iCloud and we need to remove these files as soon as we are done with them, that they do not continue to consume space on the user’s device.

Caching Data

We can save data by using .save() method.

Here, data can be an UIImage or even JSON. We will be storing data in the cache directory and we need to give a filename in this case: files.json.

We will save/cache the data after we receive it from the network call.

Let’s look at Networking.swift. But first, we need to import Disk in our Swift file. We can do that by using the import statement at the top of the file.

Saving the data

After we receive the jsonData we will save that before we encode the data , as mentioned earlier , Disk takes care of encoding/decoding so we don’t need to worry about that, we will save data as users.json.

Next, we need to change the way we load data inside our tableview.

Retrieving the data

First, we need to retrieve the data stored as users.json from .caches , we put a try which will go inside the catch block, in case, we run into any error while retrieving the data. Errors can be caused if the cache expires so we need to fetchData from a network call.

After this, we need to call reloadData() which reloads the rows and sections of the table view.

Further Reading :

Congratulations🎉! You have just completed all the tutorialsl!

All of the above-written code is available on Github. Watch this space for more interesting challenges, up next in this series!

Confused about something? Let us know in the responses below. 😄

Summing up everything

Now the series finally comes to an end. Thank you for reading up our articles and we hope that you learned something along the way.

Our journey so far :
First, we learned about programmatic UI’s and using TableView’s and building custom TableView Cells. After that, we learned about color themes and Dark Mode. We set up our NavigationBar and learn about how to handle JSON in Swift along the way. Then, came the most crucial part of development that is networking and we also set up the TabBar in our app. We learned about handling data in iOS using delegates and doing form data validation along with UIAlertControllers. Finally, we finished the series with some lessons on caching in Swift using Disks.

If you would like to learn more about iOS development using Swift.

--

--