Clean Architecture: Networking in Swift

Rahul Katariya
rahulkatariya
Published in
3 min readFeb 15, 2018

We all are moving towards a clean architecture in iOS and for this, we have been experimenting with a lot of approaches like: moving from MVC to MVVM to make our View Models independent and easy to test. More patterns like Viper and MVVM-C have also emerged and are widely adopted by developers.

In this tutorial, we will see how we can write clean network requests using Restofire.

Restofire is a Protocol Oriented Network Abstraction Layer written on top of Alamofire to use services in a declarative way

List Notes API

We will use Notes API from apiary.io — https://rahulkatariya.docs.apiary.io/

Breaking down the cURL request

  1. Hostprivate-07c21-rahulkatariya.apiary-mock.com
  2. HeadersContent-Type: application/json
  3. Pathnotes
  4. Response — A collection of NoteResponseModel

Configuration

The best place to configure Restofire is inside didFinishLaunchingWithOptions.

Once we have set the host and headers, it will be applied to all the network requests we make using Restofire.

You can provide more configuration globally like request timeout, retry codes, retry interval, maximum retry count, validation, authentication and more.

ResponseSerializer

By default, all network responses are of type Data but we can set custom response serializers according to our need. For this demo, we will make use of JSONDecodableResponseSerializer by adding the following snippet inside the application.

By adding the above code, we can now use decodable types as Response from the requests. Let’s create a decodable model for Note response.

Requestable

In Restofire, every HTTP request is independent and is represented by a Requestable protocol and provides delegate methods to handle the response. The delegate methods can be used to save the response inside CoreData or Realm or UserDefaults or you can do any custom action. Let’s create our first network request to get all notes.

We have already configured the host and headers globally in AppDelegate. Let’s create NotesGetAllService service with path as "notes" and response as [NoteResponseModel]

You may have noticed that I have passed a NSManagedObjectContext inside the requestable to store the results of the response inside CoreData by listening to the response callback as in the following code.

Now, TheNotesGetAllService is flexible enough to be called from anywhere in our code and it will store the response inside CoreData.

We can now make the request to get all the notes after setting up CoreData in AppDelegate.

The execute method is a default implementation of Requestable protocol which makes the request and calls the delegate method of NotesGetAllService.

Note: You can also pass a completion handler inside the execute method if you want the response also to be delivered with the call.

When the request is completed, it will save the list of notes inside CoreData and NotesTableViewController which is bound with FRC will automagically show the results.

A fully functioning demo including NotePostService and NoteDeleteService accompanying this article can be found here — https://github.com/Restofire/RestofireDemo

Happy Networking 😎

--

--