Swift. NetworkManager. DataLoader

Ace Rodstin
3 min readJul 10, 2022
Xcode

At least in the last decade, the number of applications that require access to the Internet has grown exponentially. Moreover, for most projects, only the execution of REST requests and the loading of images are required. In this terms, some sort of network manager is needed to download data from the internet. Next, NetworkManager will be introduced, which one sets up REST requests.

The goal

To develop a network request manager for handling REST requests. At the same time, it is necessary to be able to manage absence of a connection and caching logic. The solution should be based on the new concurrency system debuted in iOS 13.

The solution

Below is the base class, which contains the logic for handling the server response (URLResponse), from which specific loaders (modules), including DataLoader, will be inherited and which depends only on the Foundation framework.

The presence of the HTTPStatusCode enum should not be surprising, since this is a fairly standard set of constants returned by servers in a URLResponse.

Also Request.Error is the most trivial structure that contains the server’s response in case of an error.

The DataLoader is the most commonly used. Since REST requests are executed with its help, it is preferable that this module also perform serialization / deserialization of requests.

HTTPMethod — another object containing constants that define the request method. Because the methods can be non-trivial, a struct was chosen as the container instead of an enum to be extensible.

HTTPHeader — an object that contains information about the header of the request. Also implemented as a struct rather than an enum because it needs to be able to be extended with non-trivial values.

With request parameters, the solution a bit more complicated, since they can be passed both in the query string and in the body of the request. Therefore, the Parameters structure only configures how the parameters will be passed, while the logic to construct the query or body is located in the Query<T> object.

Learn more about the Query<T> structure in Swift. Serialization of request parameters article, so below is only the source code of the object.

QueryCodingKey is one of the dependencies of the Query<T> structure that encodes any Encodable value.

QueryEncoding is another Query<T> dependency that configures the serialization of the query string.

And finally the request itself. Is nothing more than a wrapper over the URLRequest object.

By now the NetworkManager kernel is completed and ready to use.

Use

The above code forms an easy-to-use abstraction, making the code much easier to read and maintain.

The implementation of the createUrl(host:path:) and accessToken(for:) methods is beyond the scope of this article, so it is not presented.

Conclusion

It is possible that Apple will introduce its concurrent of Alamofire. Moreover, Alamofire has flaws, and spaghetti code and dependence on other libraries should be cited as an example. In any case, having NetworkManager in pocket, implemented with abstraction, encapsulation and inheritance, results the developer to sit back in his chair for a moment and think about solving another problem.

  1. Original article
  2. URLSession
  3. URLRequest
  4. URLResponse
  5. Alamofire

--

--