Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

apouche commented Mar 23, 2016

Smart move using the AnonymousDisposable to cancel the request 👍

@rlam3

This comment has been minimized.

Copy link

rlam3 commented Aug 10, 2016

How would we use this with alamofire object mapper?

@Bruno125

This comment has been minimized.

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
You can’t perform that action at this time.