Writing a Generic/Reusable Networking Layer using Combine Swift iOS
Apple launched Combine in 2019 with iOS 13 and it changed the way we do asynchronous programming on iOS platform. There have been some reactive frameworks like RxSwift which people have been using, however, having something native takes away the overhead of adding an additional framework / dependency to your project.

Diving right into it.

Here we’re gonna start with a protocol containing a generic function to handle our requests.

The request function here takes in a custom request — which we’re going to talk about in a while — and returns the Publisher with the respective decoded response and error.

Now let’s add the implementation for that function:

Here is the NetworkRequest class that we use to compose our request:

Now let’s see how we can actually use this to make a request.

Before we do that — I’m going to show you how I organize my API endpoints in an enum.

Here is the Environment:

Now let’s start with creating a service which will have all our requests. This is how one service looks:

Now we can use this service from a manager or from our controller.

I’ll be talking more in future blogs about how to test these publishers and focus more on the testability aspect of this in an upcoming tutorial. Stay Tuned!
['Uday P']
['Combine', 'Development', 'iOS', 'Swift Programming']