Why is the #onComplete() called twice even though the #addSomething() is called only once? This the code snippet:
private void addSomething() {
Subscriber<AddCommentResponse> subscriber = createSubscriber();
NetworkService.getIp()
.subscribeOn(Schedulers.io())
.flatMap(addSomethingService)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}
private Subscriber<AddCommentResponse> createSubscriber() {
return new Subscriber<AddCommentResponse>() {
@Override
public void onCompleted() {
this.unsubscribe();
Toast.makeText(
MyApplication.context, getString(R.string.toast_comment_added_successfully), Toast.LENGTH_LONG
).show();
navigateBack();
}
@Override
public void onNext(AddCommentResponse response) {
onCompleted();
}
};
RxJava follow this contract : it will call on your observer onNext then will end your stream with onComplete OR onError call.
You don't have to do it. RX will do it for you.