Write a Network Layer in Swift

Or just take mine

Image for post
Image for post
Photo by C Dustin

I previously produced a network manager. This article explains it. Good oh!

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

Prerequisites:

  • it would be easier if you knew something about type erasure
  • To install the network manager to which this refers, you will need to know something about Swift Package Manager

Terminology:

Type Erasure: Removing type information by wrapping a type

The public API

There are two entry points for this network manager — both AnyNetworkManager and NetworkManager are publicly accessible. Not only that - there is a MockNetworkManager that is publically avaliable for testing.

Let us first look at NetworkManager. Let us take a look at the exposed protocol:

The extension allows us to have default arguments , and the associated type has it’s own article.

We can then access the network manager using a token, header and data:

or without those parameters

As we can see this is using a MockURLSession to ensure that we are not using the actual URLSession with our network manager - ensuring that we will never actually make network calls using the (well...) network - but using the following MockURLSession (which in turn has associated types and it's own MockURLSessionDataTask)

Usually I would prefer to use the following AnyNetworkManager which it the type erased version - with the advantage that we can save the type in something like the following:

which means that we don’t have to add an associated type into the containing class! This is great!

So let us look at the AnyNetworkManager, which of course also conforms to NetworkManagerProtocol.

We can see that AnyNetworkManager has an associated type which is the URLSession (that itself conforms to the URLSessionProtocol). Essentially we store the functions that the NetworkManager can call as closures, and then call them when we are required to.

Now when we test we can slot a MockNetworkManager right into the AnyNetworkManager which is defined as follows:

Note that we can instantiate the actual URLSession into the MockNetworkManager as we never actually try to make any network calls!

Here is what a test might look like:

we can even swap out the data that the mock network will output! That is rather awesome!

Using tests

In order to test the network manager, the package can be downloaded and ⌘-U will run the tests.

Conclusion

Want to look at the whole thing? network manager has the whole thing, tests an all.

Want to use it? Download it and off you go!

I’m now using this in many of my articles: rather than explaining the working of the network. I recommend that you use this network manager (I like it!), and hope that it can save you some time and help you in your programming journey.

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