Create a Network Manager using Swift Generics & Codable protocol

atul khatri
3 min readJul 12, 2021

(Part 4 of 8)

This is the fourth part of our tvOS Bootcamp. Please check out the previous pages to know more about this series.

Network Manager

There are multiple ways to fetch data. Most apps use third party libraries like AFNetworking or a wrapper framework like Moya to get the work done. These are very good libraries, which provide an easier & familiar way of managing networking in our apps.

In recent years, URLSession API has become very mature and easy to use. Specially with the introduction of Codable support in Swift, it has become really easy to serialize JSON data into iOS apps. Thus, we will not use any third party framework for networking.

For this bootcamp, we are going to create a very simple Network Manager, which only has 2 methods. 1st to fetch a list of Movies & 2nd to fetch a list of Series.

Firstly, we would create a new file named NetworkManager.swift, this file will contain a struct named NetworkManager.

URLSession class takes an instance of URLRequest and executes it to fetch respective data.

Let’s create a method which will return us an instance of URLRequest from a url string.

Above method takes a url string & creates an instance of URLRequest providing it a URL instance.

It also sets the httpMethod to “GET” since both APIs use GET method. We can take method as a parameter of this function but for the sake of simplicity, let’s hardcode it within the function.

Secondly, we set some header fields for URLSession to know that we are expecting a JSON data from the server.

Now that we have an instance of URLRequest , we need to execute the network request by using URLSession. Below method takes care of the execution.

This method takes 2 arguments. 1st argument is an instance of URLRequest we created before, 2nd argument is the completion handler, which gets called when URLSession has finished execution.

Since we would like to use this function to work for any kind of models (as long as these conform to Codable), we have made it dynamic using swift Generics.

We have created an instance of URLSession by using default configuration. This default configuration uses system defined policies for timeout, caching, cookies etc.

Next step is to create a URLSessionDataTask.

This task could be started by resume() method, it could also be suspended or cancelled if need be.

For our simple use-case, we will just resume this task and forget about it.

Once we get the data in our completion block, we will then convert this data using JSONDecoder into respective type by using T.self.

Final part of this structure is to create 2 functions for fetching data for Movies & Series. This is how we will create them:

We just create a request using createRequest method we made earlier and then call executeRequest to execute the newly created request.

In case API returns error, we are sending a custom instance of Errorwe specified simply like this:

To hold data in native models, we have created a couple of structs.

As its name suggest, PageModel holds array of carousel data for the whole page.

RailModel represents a single Carousel of data. It contains a title, and a list of AssetModel each representing a single Movie or Series in its multiple properties.

Now that we have this Network manager ready. we can simply go ahead an fetch our data inside HomeViewController like this:

In next tutorial, we will see how to render multiple carousels using this data.

https://atulkhatri.medium.com/create-streaming-app-home-screen-on-tvos-e857e9d669ca

--

--

atul khatri

Software Engineer (iOS) @ Meta | Tech Enthusiast | Learner