Questa è una domanda in 2 parti, la prima è simile a questa domanda: Proper usage of the Alamofire's URLRequestConvertible. Ma ho bisogno di un piccolo aiuto in più!Modello Singleton e uso corretto di URLRequestConvertible di Alamofire
1) Creo un router enum che implementa URLRequestConvertible per ciascun modello nel mio livello di modello?
La pagina alamofire github fornisce un esempio di un router che ho copiato qui:
enum Router: URLRequestConvertible {
static let baseURLString = "http://example.com"
static var OAuthToken: String?
case CreateUser([String: AnyObject])
case ReadUser(String)
case UpdateUser(String, [String: AnyObject])
case DestroyUser(String)
var method: Alamofire.Method {
switch self {
case .CreateUser:
return .POST
case .ReadUser:
return .GET
case .UpdateUser:
return .PUT
case .DestroyUser:
return .DELETE
}
}
var path: String {
switch self {
case .CreateUser:
return "/users"
case .ReadUser(let username):
return "https://stackoverflow.com/users/\(username)"
case .UpdateUser(let username, _):
return "https://stackoverflow.com/users/\(username)"
case .DestroyUser(let username):
return "https://stackoverflow.com/users/\(username)"
}
}
// MARK: URLRequestConvertible
var URLRequest: NSURLRequest {
let URL = NSURL(string: Router.baseURLString)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue
if let token = Router.OAuthToken {
mutableURLRequest.setValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
switch self {
case .CreateUser(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .UpdateUser(_, let parameters):
return Alamofire.ParameterEncoding.URL.encode(mutableURLRequest, parameters: parameters).0
default:
return mutableURLRequest
}
}
}
Quando guardo questo (io sono nuovo a una rapida quindi per favore abbiate pazienza con me> _ <) I vedere le operazioni su un oggetto utente; stanno creando un utente, aggiornando un utente, ecc ... Quindi, se avessi oggetti modello, azienda, posizione nel mio livello di modello, dovrei creare un router per ogni oggetto del modello?
2) Quando si interagisce pesantemente con un'API, sono abituato a creare un singleton "network manager" per astrarre il livello di rete e contenere intestazioni e baseur per tale API. L'alamofire ha un "Gestore" qui descritta:
di livello superiore metodi di convenienza come Alamofire.request utilizza un'istanza condivisa di Alamofire.Manager, che è configurato con il default NSURLSessionConfiguration. Come tale, le due istruzioni seguenti sono equivalenti:
Alamofire.request(.GET, "http://httpbin.org/get")
let manager = Alamofire.Manager.sharedInstance
manager.request(NSURLRequest(URL: NSURL(string: "http://httpbin.org/get")))
è questo manager che cosa dovrei usare il mio Singleton? Se è così, come faccio a impostare la baseurl sul gestore? Inoltre, se utilizzo questo gestore, questo può/può funzionare insieme al costrutto del router mostrato sopra (con ogni impostazione dell'oggetto modello è baseurl e NSURLRquest)? Se è così puoi fornire un semplice esempio?
Sono nuovo nella libreria Alamofire e veloce. Quindi, so che ci sono molti buchi nella mia comprensione, ma sto solo cercando di capire il meglio che posso! Qualsiasi informazione aiuta. Grazie.
Hey @cnoon! Questo è molto utile, grazie! Il modo in cui questo è impostato, è possibile fare qualcosa come 'func readObjectPath (identificatore: String) -> String {return" \ (rootPath)/\ (identificatore) /? RandomParameter = true "}'.Ho tentato di farlo, ma quando trasformo l'URL in una mutevoleURLRequest, formatta il punto interrogativo in modo simile a questo: '% 3F' – Thomas
Nevermind, l'ho risolto cambiandolo in modo che il percorso fosse aggiunto alla' baseURLString' all'interno del ' URL', anziché in 'mutableURLRequest' – Thomas