Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
extension Alamofire.Request {
func processMap<N>(map:(AnyObject)->N?) -> Observable<N> {
return Observable.create { observer in
self.responseData { response in
let result = response.result
if result.isSuccess {
guard let o = map(result.value!) else{
observer.on(Event.Error(ObjectMapError.MapError(result.value)))
return
}
observer.on(Event.Next(o))
observer.on(Event.Completed)
}else{
observer.on(Event.Error(result.error!))
}
}
return AnonymousDisposable{
self.cancel()
}
}
}
func rx_responseArray<T:Mappable>(type:T.Type) -> Observable<[T]> {
return self.processMap{ Mapper<T>().mapArray($0)}
}
func rx_responseObject<T:Mappable>(type:T.Type) -> Observable<T> {
return self.processMap{ Mapper<T>().map($0) }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.