SunnySydeUp
SunnySydeUp

Reputation: 6790

RxJava2 subscribe is not called when mapping observable to completable

I have a click event which needs to make a network request.

RxView.clicks(button)
    .flatMapCompletable({ x -> networkCall() })
    .subscribe(...)

The click is an Observable.
networkCall returns a Completable.
However the block inside subscribe is never called when i tap the button.

I've also tried

RxView.clicks(button)
    .flatMap({ x -> networkCall().toObservable<Void>() })
    .subscribe(...)

How can I get this to work so that each time I tap on the button, a network request is made and is then handled in the subscribe.

EDIT:

I haven't done the network stuff yet so currently it's just

public Completable networkCall() {
    Completable.complete();
}

So it's guaranteed to complete.

Upvotes: 3

Views: 4157

Answers (1)

akarnokd
akarnokd

Reputation: 69997

The flatMap case needs items, otherwise its onComplete will never fire due to the already mentioned never-completing clicks source. For example:

RxView.clicks(button)
.flatMap({ x -> networkCall().andThen(Observable.just("irrelevant")) })
.observeOn(AndroidSchedulers.mainThread()) 
.subscribe({ v -> System.out.println(v)}, { e -> e.printStackTrace() })

Upvotes: 10

Related Questions