1

I'm learning RxAlamofire and I faced following issue: in this snippet fragment of code from onNext, onError and onCompleted doesn't get invoked, as if the subscription wasn't working at all. To compare, piece of code with normal Alamofire works just fine.

private func test() {
    let disposeBag = DisposeBag()

    Observable.from(["1", "2", "3"])
        .subscribe(onNext: {
            print("\($0) lol")
        })
        .disposed(by: disposeBag)

    let observable: Observable<(HTTPURLResponse,Any)> =
        RxAlamofire.requestJSON(.get, "https://api.fixer.io/latest?base=EUR&symbols=USD")
        .debug()

    observable
        .subscribe(
            onNext: { [weak self] (r, json) in
                print(json)
            }, onError: {
                print($0)
        })
        .addDisposableTo(disposeBag)

    Alamofire.request("https://api.fixer.io/latest?base=EUR&symbols=USD")
        .responseJSON {
            print($0)
        }
}
AndrzejZ
  • 245
  • 2
  • 10
  • Probably the subscription is disposed before the request finishes its job because the variable `disposeBag` was defined as a local variable and it was garbage collected. Try defining it outside of `test()`, preferably as a class variable and you should see subscription being executed as expected. – Ozgur Vatansever Mar 16 '17 at 23:03
  • Thank you buddy, that did the trick. – AndrzejZ Mar 17 '17 at 14:06

0 Answers0