ReactiveX / RxAndroid

RxJava bindings for Android
Apache License 2.0
19.89k stars 2.94k forks source link

BackpressureStrategy.LATEST result is different in setOnClickListener #432

Closed JingweiWang closed 6 years ago

JingweiWang commented 6 years ago

I use the following code to run :

findViewById(R.id.button_latest).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Flowable
                        .create(new FlowableOnSubscribe<Integer>() {
                            @Override
                            public void subscribe(FlowableEmitter<Integer> emitter) throws Exception {
                                for (int i = 0; i < 1000; i++) {
                                    emitter.onNext(i);
                                }
                            }
                        }, BackpressureStrategy.LATEST)
                        .subscribeOn(Schedulers.newThread())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Consumer<Integer>() {
                            @Override
                            public void accept(Integer integer) throws Exception {
                                Log.d("LATEST", integer + "");
                            }
                        }, new Consumer<Throwable>() {
                            @Override
                            public void accept(Throwable throwable) throws Exception {
                                Log.d("LATEST", "Throwable: " + throwable.getMessage());
                            }
                        });
            }
        });

I expected result is printed out 0 to 127 and 999.

But the result of the matter is not like this.

Why is it like this?

akarnokd commented 6 years ago

Please see the JavaDocs of subscribeOn.

If there is a create(FlowableOnSubscribe, BackpressureStrategy) type source up in the chain, it is recommended to use subscribeOn(scheduler, false) instead to avoid same-pool deadlock because requests may pile up behind an eager/blocking emitter.

JakeWharton commented 6 years ago

Closing as it's not a bug or feature request for RxAndroid. For usage questions please consider the RxJava mailing list or StackOverflow with the 'rx-java' tag in the future.

JingweiWang commented 6 years ago

@akarnokd Thank you but the result is still wrong.

akarnokd commented 6 years ago

Please ask this question on Stackoverflow where you specify what you expected.