Android accumulate some knowledge about RxJava2.0

Disclaimer: This article is a blogger original article, shall not be reproduced without the bloggers allowed. https://blog.csdn.net/Coder_Hcy/article/details/78819648

Recently I stumbled around "Taobao (look for the code)" and found rxjava become a rxjava2.

 

1 changes the code:

Observable.create(new ObservableOnSubscribe<String>() {
            @Override
            public void subscribe(ObservableEmitter<String> e) throws Exception {
                e.onNext("1");
                e.onNext("2");
                e.onNext("3");
                e.onNext("4");
                e.onComplete();
                e.onNext("5");

            }
        }).subscribe(new Observer<String>() {
            @Override
            public void onSubscribe(Disposable d) {
                dis=d;
                Log.i("Rxjava2::onSubscribe","d");

            }

            @Override
            public void onNext(String o) {

                Log.i("Rxjava2::onNext",o);
                if (o.equals("2")){
                    dis.dispose();
                }
                tv.setText(o);
            }

            @Override
            public void onError(Throwable e) {
                Log.i("Rxjava2::onError",e.getMessage().toString());

            }

            @Override
            public void onComplete() {
                Log.i("Rxjava2::onComplete","c");

            }
        });

Seemingly more:

 

ObservableEmitter: for transmitting data to a viewer. (I think there can be understood as an observer [server] [observer] relationship with the client is sent an observer to observer onnext (), onerror, oncomplete three kinds of instruction, but onnext can carry parameters), where clear that subcrible the code in the pit will certainly be executed, regardless of the observer would be willing to receive, as long as the bindings, it will send, but the viewer can choose not to accept.

Disposable: the observed data to send an observer, if the observer how do not want to take, you have to call to dispose () method, of course, is also an observer can interrupt the viewer to accept the data, just call oncomplete or onerror method, and this rxjava1.x is the same

2 changes: action1 function1 looks nothing like a callback.

Special episode: personal development time rxjava1.x thief would like to use the other not and do not write callback action1, see no need. But the lesson of blood is experiencing this bug:

java.lang.IllegalStateException: Exception thrown on Scheduler.Worker thread. Add `onError` handling.

Caused by: rx.exceptions.OnErrorNotImplementedException

Caused by: rx.exceptions.MissingBackpressureException

Probably the reason is: less onerror callback process (reduction scenarios: a black screen, then after a while according to press on, to jump) [wrong] personal views

Pit father Index: Ni Mala down the log saw, what line do not report an error

The solution: use of local rxjava with lamb plus onerror process? (Seemingly did not collapse then pan)

Of course, my idea is absolutely wrong to force, after reading the great works of God:

Play with him

The code:

 

        Observable.just("ddd").subscribe(new Consumer<String>() {
            @Override
            public void accept(@NonNull String s) throws Exception {

            }
        });

Added a seemingly thrown, at first glance feel 6 (messed up).

 

 

  Observable.just("ddd").subscribe((msg)->{Log.i("接收的值",msg);},Throwable::printStackTrace);

Or write like rxjava1 the same?

 

 

  Observable.just("ddd").subscribe((msg)->{Log.i("接收的值",msg);},(error)->{Log.i("error",error.getMessage());});


 

Retrofit2 + Rxjava2 changes

 

        Retrofit adapter = new Retrofit.Builder()
                .baseUrl(endpoint)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .build();
        return adapter.create(serviceClass);

 

// use rxjava2 polling, polling limited number of times, once every 0.1s polling, polling a total of 20 times.

 Flowable.interval(0, 100, TimeUnit.MILLISECONDS)
                .take(20)
                .subscribeOn(Schedulers.io())
                .subscribe(new Subscriber<Long>() {
                    @Override
                    public void onSubscribe(Subscription s) {
                        s.request(100);
                        mCheckUpdate = Disposables.fromSubscription(s);
                    }

                    @Override
                    public void onNext(Long aLong) {
                        if (isNetworkAvailable()) {
                            processor.onNext("checkUpdate");
                            mCheckUpdate.dispose();
                        }
                    }

                    @Override
                    public void onError(Throwable t) {


                    }

                    @Override
                    public void onComplete() {
//                        getAssociated();


                        showDialog();
                    }
                });

 

 


 

 

 

 

 

Guess you like

Origin blog.csdn.net/Coder_Hcy/article/details/78819648