Member-only story

Cache URLResponses in Swift

We don’t want to preload every ViewModel!

Steven Curtis
3 min readOct 15, 2020

Photo by Abel Y Costa

Difficulty: Beginner | Easy | Normal | Challenging
This article has been developed using Xcode 12, and Swift 5.3

Prerequisites:

* You will be expected to make a Single View Swift Application
* This code is built on my network manager, which you can download and use for free
* This involves subscripts that I’m covered in Create your own subscript
* This code uses Singletons

Terminology

Cache: a collection of items of the same type stored

The motivation

Alamofire has a cache. Surely I can make a simlar feature for my own Network manager? (Spoiler: Yes, yes I can).

This allows use of dependency injection and provides us with a suitable way of testing our code. That is great!

Note that I’ve left print logging in the code that I’ve checked in, this is so we can see when data is stored and used from the cache by looking at the console. You wouldn’t want to do that in production code (so please don’t!)

Coding to an interface

--

--

No responses yet