Better networking with Moya + RxSwift

mcontin

Mattia

Posted on December 5, 2019

Better networking with Moya + RxSwift

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 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.


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.


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.


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


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.

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:



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


Enter fullscreen mode Exit fullscreen mode

After this you will start seeing calls being logged in the console. You can also make your own plugin! Find more about it here.

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:

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!

💖 💪 🙅 🚩
mcontin
Mattia

Posted on December 5, 2019

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related