Better networking with Moya + RxSwift
Tags: ios, swift, rxswift, moya
Published: 2019-12-05 19:22:07

![](https://cdn-images-1.medium.com/max/2868/1*lrIeU_YrGry3rDKH1tLnjw.png)

If you’re here, you’re problably an iOS developer and chances are you needed to implement some REST calls in your apps.
Unless you’re a fan of DIY, you’ve probably used the most popular networking library that is Alamofire. I’ve done this too.

It can easily get messy with Alamofire so today we're going to try Moya, an Alamofire wrapper that encourages you to split your service definition in a different place from the actual implementation.
And we're going to do this with RxSwift to make our code cleaner.

We’ll keep this basic and use some [stub API](https://jsonplaceholder.typicode.com) as our endpoint and we’re going to GET and DELETE some posts.

Let’s get started, shall we?

---

## Installation

First of all, we need to install Moya+RxSwift with Cocoapods:
In your Podfile add:
> pod 'Moya/RxSwift', '~> 12.0'

And then, in the terminal run:
>  pod install

and we’re done! You can find other installation methods on the repo’s README [here](https://github.com/Moya/Moya).

---

We’ll start by defining the endpoints that we’re going to use.

To do so, we need an enum with a case for each operation.
If we need to pass parameters to our call, we can pass them to the enum case.

{% gist https://gist.github.com/mcontin/df07d5e1dbdeec277223df6d9c649cd6 %}

---

Let’s shape up our service by implementing Moya’s TargetType.

This protocol will require us to define how each call should be built by Moya.

{% gist https://gist.github.com/mcontin/e7e4429332c372cdf515b666028fac31 %}

---

Ok, we’re done with our service specifications and now we just need to implement the calls. Now it’s RxSwift’s turn.

{% gist https://gist.github.com/mcontin/fd60599db6b4676ab2155ebff26c7250 %}

---

We need to actually consume the calls right? We’ll do that right now.

For this example we’ll return a Completable, to keep it simple, and in the `onSuccess` we manage the data parsed from the server response.

{% gist https://gist.github.com/mcontin/e15e18f5ff6dbc5cdf8c74f1360eca2f %}

We’re done with our network part and now you just need to handle the results of the calls.

How was it? Setting up your network layer with Moya is a bit more verbose than with Alamofire but investing a little more time will pay back in the long run, and adding some RxSwift only makes it better.

---

Let’s spice up our networking layer.

We’re about to add every developer’s two favourite things: logging and error handling.

## Logging

This is the best thing ever and it’s also the easiest since Moya comes with a built-in logger, which can be setup like this:

```swift
MoyaProvider<ForumService>(plugins: [NetworkLoggerPlugin(verbose: true)])
```

After this you will start seeing calls being logged in the console. You can also make your own plugin! Find more about it [here](https://github.com/Moya/Moya/blob/master/docs/Plugins.md).

## Error handling

Last but not least, error handling. You know how to handle your errors so I’m not going to teach you that, but since you probably use custom enums for error representation, you have to figure out what happened during the call and return the appropriate info in order to tell the user what went wrong.

Let’s see how we can handle errors in our network layer:

{% gist https://gist.github.com/mcontin/a21698aa216e7d86989ad624e4095147 %}

When handling errors, you will just need to cast the error you receive to the one(s) you expect.

## Conclusions

I hope you enjoyed this different solution. Some developers are against adding another layer to their network stack, but I find this a good compromise between having a messy network manager and having to architecture, test and maintain your own layer.

If you have comments or suggestions please don't be shy!