Skip to content

Instantly share code, notes, and snippets.

@mbalex99
Last active May 22, 2022 11:58
Show Gist options
  • Save mbalex99/8802db1695f20c520ca0 to your computer and use it in GitHub Desktop.
Save mbalex99/8802db1695f20c520ca0 to your computer and use it in GitHub Desktop.
Alamofire and RxSwift
let rx_request = Observable<Value>.create { (observer) -> Disposable in
let requestReference = Alamofire.request(.POST, url, parameters: payload)
.responseJSON(completionHandler: { (response) in
if let value = response.result.value {
observer.onNext(value)
observer.onCompleted()
}else if let error = response.result.error {
observer.onError(error)
}
})
return AnonymousDisposable {
requestReference.cancel()
}
}
@apouche
Copy link

apouche commented Mar 23, 2016

Smart move using the AnonymousDisposable to cancel the request 👍

@rlam3
Copy link

rlam3 commented Aug 10, 2016

How would we use this with alamofire object mapper?

@Bruno125
Copy link

Bruno125 commented Nov 5, 2016

AnonymousDisposable was deprecated in the latest version. I found this on the changelog:

* Deprecates AnonymousDisposable in favor of Disposables.create(with:)

Replace it with this and it will work:

return Disposables.create(with: { requestReference.cancel() })

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment