Cache URLResponses in Swift

We don’t want to preload every ViewModel!

Image for post
Image for post

Photo by Abel Y Costa

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



The motivation

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

That protocol can be the following:

Now the reason for doing this is to create a mock that we can later swap out during tests — for my rather basic tests I’m not concerted about the subscript returning Data, for more complex tests this would need to be filled in.

speaking of tests, we can think of creating those to test whether the data can be restored from the cache, and this may be something like the following:

If we get the data from the cache or from the Network the code shouldn’t care. I’ve produced two different tests to demonstrate this:

So in the first test the data comes from the cache — and we check that the network manager returns that data (it does), in the second test the data comes from the network, and once again all is fine (which is great!

The altered Network Manager

Now when this is called from the code, I’d say it calls it as normal from my network manager code, which is something like the following:

Which of course uses a force-unwrap which you shouldn’t do in production code but then you can deal with the response as you might well usually do.

The whole code is in the repo, which should answer the many implementation details that you would like to confirm with me.


Swapping out implementation details using dependency injection is really important

In any case, have a nice day.

If you’ve any questions, comments or suggestions please hit me up on Twitter

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store