Generic Network Layer in iOS Development with Swift

Yusuf Demirci
Apr 19, 2020 · 4 min read

Problem

Struggling in the network layer when sending requests and parsing responses.

Solution

Applying generic, modular network layer. The solution includes Generic Type, Completion, Singleton, Codable, URLSession and OOP(Object Oriented Programming) terms.

According to most of the mobile developers, the number one subject or layer or structure in mobile development is Network Layer. A lot of mobile applications work online then the Network Layer is the hearth of them. The point is, how can we build a strong network layer against even for complex applications? Let’s find the answer!

What are we going to do?

We are going to build a service manager, request and response classes, a middle layer to communicate between controller and service calls and data models. All are very similar for every developer but the difference is our response classes are generic. Also, the structure is modular and we can extend it easily.

Controller: The view that needs data and where the service call begins.

Middle Layer: The layer communicates between the Controller and the Service Manager. It defines request and response class.

Service Manager: The base class sends a request and gets a raw response. It parses the response and sends it to the Middle Layer.

Request Model: The model that has information about the endpoint requirements such as endpoint path, request headers, URL parameters, request body.

Response Model: Contains base structure of JSON such as isSuccess, message and data(generic).

I finished a working example of the network layer. Let’s talk over the project! You can download the project here → https://github.com/demirciy/GenericNetworkLayeriOS

I have created custom response JSON which is called Response.json in the Sources folder. Also, the project has codes for real REST service. The chosen REST service is Current Average Price provides by Binance public API. The service returns average price by minute. The Response.json has the same response.

Let’s start where we call the service.

When the app launches, the HomeController runs first. There is no code related to UI. We are just working on service calls.

HomeController.swift

The service wants us currency pairs in the parameters. We send ‘BTCUSDT’ as the value of the symbol key. By the way, the Services class is the Middle Layer on the Network Layer.

Why we need Middle Layer?

The Middle Layer is a bridge between the Controller and the Service Manager. We can integrate offline mod into the app. By then, we check online/offline status on the Middle Layer and get data by the network status.

Services.swift

The Services class includes class functions which represent an endpoint with its request and response model. Before seeing ServiceManager, we must see what is going on inside RequestModel and ResponseModel.

The RequestModel has variables what a request needs.

RequestModel.swift

On the other hand, every endpoint has own request and response classes. The request class inherits from RequestModel which is the base request class. We have AveragePriceRequestModel inherits from the RequestModel.

AveragePriceRequestModel.swift

It overrides path and parameters variables that are unique for the Average Price service.

ResponseModel.swift

The ResponseModel has a few variables to read returning JSON. The data variable is a generic type so that we can use a different kind of response models via generic type. In software development, most of the services have a base response structure which is very similar to that,

Basically, isSuccess represents the status of the request, message represents a text the user might see or an error message, data represents a JSON object the client uses.

Response.json
AveragePriceResponseModel.swift

We use Codable in the response model. Also, we can see what the response is on the 7th line.

Service Manager

It is the heart of the Network Layer. I have preferred to use URLSession which is the class provides by Apple in the Foundation framework for service communication. By the way, when decoding the Response.json, we do not need URLSession.

ServiceManager.swift

The Swift class has an enum for us which is Result. It has 2 cases, success and failure. We return generic response model(AveragePriceResponseModel) on the success. On the other hand, we return ErrorModel on the failure.

Archived — Flawless iOS

Archive of Flawless iOS publication — no longer accepting submissions

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

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