In this article, we will learn how to architect and create an efficient and easy to use networking layer by leveraging Combine publishers, Codable, CustomStringConvertible protocol, and a custom Endpoint struct.

At the end of the tutorial, you will have a ready-to-use implementation that you can easily copy and paste in your app and expand it the way you want.

The full source code is available at the bottom of the article.


Let’s Start

Before we dive into the code, let’s first outline all components of our networking layer:

  • Endpoint — a handy struct that has path and queryItems properties. By defining extensions on it, we can conveniently create a base URL of our REST APIs, define specific endpoints and headers (that will make more sense to you as we start implementing it, so don’t worry).

  • Codable models — simple objects that can be created from JSON.

  • Network Controller — responsible for working directly with URLSessionTasks and decoding a Codable model from the data. For example, it has a generic get() method that returns a publisher containing a model.

#swift #mobile #ios #xcode

Implement a Networking Layer Using Combine in Swift 5
26.30 GEEK