2

Here's an example of an Alamofire router

import Foundation
import Alamofire

enum PostRouter: URLRequestConvertible {
  static let baseURLString = "http://jsonplaceholder.typicode.com/"

  case Get(Int)
  case Create([String: AnyObject])
  case Delete(Int)

  var URLRequest: NSMutableURLRequest {
    var method: Alamofire.Method {
      switch self {
      case .Get:
        return .GET
      case .Create:
        return .POST
      case .Delete:
        return .DELETE
      }
    }

    let params: ([String: AnyObject]?) = {
      switch self {
      case .Get, .Delete:
        return (nil)
      case .Create(let newPost):
        return (newPost)
      }
    }()

    let url:NSURL = {
      // build up and return the URL for each endpoint
      let relativePath:String?
      switch self {
      case .Get(let postNumber):
        relativePath = "posts/\(postNumber)"
      case .Create:
        relativePath = "posts"
      case .Delete(let postNumber):
        relativePath = "posts/\(postNumber)"
      }

      var URL = NSURL(string: PostRouter.baseURLString)!
      if let relativePath = relativePath {
        URL = URL.URLByAppendingPathComponent(relativePath)
      }
      return URL
    }()

    let URLRequest = NSMutableURLRequest(URL: url)

    let encoding = Alamofire.ParameterEncoding.JSON
    let (encodedRequest, _) = encoding.encode(URLRequest, parameters: params)

    encodedRequest.HTTPMethod = method.rawValue

    return encodedRequest
  }
}

What this basically does is create,delete or get Posts. This code looks clean for a simple app that does just this. What about an app that has large amount of endpoints and models ? Let's just say we have to do different operations on entities like Users,Companies, Payments, Comments, Channels, etc. I'm talking about large number of endpoints and methods.

In this case is it necessary to create one router for each endpoint ? It doesn't feel like , is it a good practice to use same router class for all these endpoints.

A similar question has been asked here : Proper usage of the Alamofire's URLRequestConvertible

But I'm still not satisfied with the answers to above question since accepted answer suggests to use same router for all endpoints. What is the best practice here?

Community
  • 1
  • 1
Rukshan
  • 7,902
  • 6
  • 43
  • 61
  • I'm hoping after a year you found an answer to this... What did you come up with? Can you share as an answer to this question? I'm sure a lot would benefit from it... – Jan Apr 11 '18 at 20:39

1 Answers1

-1

First of all, you should never send requests form View Controllers. This is just too "smelly".

As to your question, there can be no definite answer. Networking is a large enough task to be implemented as a separate layer in your app architecture. How to implement it... This is really a hard question. I guess, there are as many ways to do it, as many developers there are. It also depends on the app architecture pattern you are using (MVC, MVP, VIPER, MVVM etc.)

As for me, I always do networking via services. I have a number of classes that encapsulate addressing AFNetworking/Alamofire. And only devoted services address those classes (one service per one concept, e.g. Authorization Service, User Service, Product Service, Whatever-Else Service).

I certainly recommend you to investigate this question. You might start from reading about architecture patterns I mentioned above. Then - specifically about Networking layer. To start with - this might be helpful:

https://talk.objc.io/episodes/S01E01-networking

That video once brought me to some ideas I am using now. But remember - no networking calls in your view controllers =))

Yevgeniy Leychenko
  • 1,287
  • 11
  • 25
  • 1
    My question is rather deep and it's indeed about a separate layer (and even further, an abstraction layer where even Alamofire isn't glued to that separate network layer). This is one more level further than what you are talking about. So my question is about best practices when using Alamofire Routers to implement a network abstraction layer. – Rukshan Feb 21 '17 at 18:39