<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Mattia</title>
    <description>The latest articles on Forem by Mattia (@mcontin).</description>
    <link>https://forem.com/mcontin</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F268922%2F386d4083-fd32-4874-a990-706b10b55f40.jpg</url>
      <title>Forem: Mattia</title>
      <link>https://forem.com/mcontin</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/mcontin"/>
    <language>en</language>
    <item>
      <title>Better networking with Moya + RxSwift</title>
      <dc:creator>Mattia</dc:creator>
      <pubDate>Thu, 05 Dec 2019 19:22:07 +0000</pubDate>
      <link>https://forem.com/mcontin/better-networking-with-moya-rxswift-1mlk</link>
      <guid>https://forem.com/mcontin/better-networking-with-moya-rxswift-1mlk</guid>
      <description>&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2868%2F1%2AlrIeU_YrGry3rDKH1tLnjw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn-images-1.medium.com%2Fmax%2F2868%2F1%2AlrIeU_YrGry3rDKH1tLnjw.png"&gt;&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;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.&lt;br&gt;
And we're going to do this with RxSwift to make our code cleaner.&lt;/p&gt;

&lt;p&gt;We’ll keep this basic and use some &lt;a href="https://jsonplaceholder.typicode.com" rel="noopener noreferrer"&gt;stub API&lt;/a&gt; as our endpoint and we’re going to GET and DELETE some posts.&lt;/p&gt;

&lt;p&gt;Let’s get started, shall we?&lt;/p&gt;


&lt;h2&gt;
  
  
  Installation
&lt;/h2&gt;

&lt;p&gt;First of all, we need to install Moya+RxSwift with Cocoapods:&lt;br&gt;
In your Podfile add:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;pod 'Moya/RxSwift', '~&amp;gt; 12.0'&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;And then, in the terminal run:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;pod install&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and we’re done! You can find other installation methods on the repo’s README &lt;a href="https://github.com/Moya/Moya" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;



&lt;p&gt;We’ll start by defining the endpoints that we’re going to use.&lt;/p&gt;

&lt;p&gt;To do so, we need an enum with a case for each operation.&lt;br&gt;
If we need to pass parameters to our call, we can pass them to the enum case.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;p&gt;Let’s shape up our service by implementing Moya’s TargetType.&lt;/p&gt;

&lt;p&gt;This protocol will require us to define how each call should be built by Moya.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





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


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;





&lt;p&gt;We need to actually consume the calls right? We’ll do that right now.&lt;/p&gt;

&lt;p&gt;For this example we’ll return a Completable, to keep it simple, and in the &lt;code&gt;onSuccess&lt;/code&gt; we manage the data parsed from the server response.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


&lt;p&gt;We’re done with our network part and now you just need to handle the results of the calls.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;p&gt;Let’s spice up our networking layer.&lt;/p&gt;

&lt;p&gt;We’re about to add every developer’s two favourite things: logging and error handling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Logging
&lt;/h2&gt;

&lt;p&gt;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:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;

&lt;span class="kt"&gt;MoyaProvider&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;ForumService&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;NetworkLoggerPlugin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;verbose&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;)])&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;After this you will start seeing calls being logged in the console. You can also make your own plugin! Find more about it &lt;a href="https://github.com/Moya/Moya/blob/master/docs/Plugins.md" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  Error handling
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;Let’s see how we can handle errors in our network layer:&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;



&lt;p&gt;When handling errors, you will just need to cast the error you receive to the one(s) you expect.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusions
&lt;/h2&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;If you have comments or suggestions please don't be shy!&lt;/p&gt;

</description>
      <category>ios</category>
      <category>swift</category>
      <category>rxswift</category>
      <category>moya</category>
    </item>
  </channel>
</rss>
