AlamofireObjectMapper + RxSwift
Closed this issue · 3 comments
mapo80 commented
Hi, is there any example for RxSwift?
Thanks!
tristanhimmelman commented
Unfortunately I don't have an example using RxSwift as I don't use that framework. Good luck finding one
hernangonzalez commented
FWIW, I started building mine, so far I use it like this:
// MARK: - Request helpers
func rx_requestObject<T: Mappable>(API: MyAPIEnum) -> Observable<T> {
return rx_requestObject(API, keyPath: API.keyPath)
.doOnError { error in
if let httpError = error as? HTTPError {
ErrorHandler.handleAPIError(httpError)
}
}
}
func rx_requestObject<T: Mappable>(URLRequest: URLRequestConvertible, keyPath: String?) -> Observable<T> {
return Observable<T>.create { observer in
var request = Alamofire.request(URLRequest)
#if DEBUG
request = request.log(level: .Verbose)
#endif
request.validate().responseObject(keyPath: keyPath) { (response: Response<T, NSError>) in
guard let value = response.result.value else {
let error = HTTPError(response: response.response)
observer.onError(error)
return
}
observer.onNext(value)
observer.onCompleted()
}
return AnonymousDisposable {
request.cancel()
}
}
}
// MARK - Empty response
final class EmptyResponse: Mappable {
init?(_ map: Map) {}
func mapping(map: Map) {}
}
ozgur commented
I created a gist for this:
https://gist.github.com/ozgur/5b2c9e6d2964ddb091c7b1b94f5c2932