The word "everyday" - RxJava

Posted by flyersman on Fri, 03 Jan 2020 20:14:48 +0100

1 life example

1.1 hypothetical scenario

At the peak of the meal, it is necessary to call and line up in a very hot restaurant. At this moment, we have got the number

1.2 operation process

We listened. When the little sister called, not our number, we kept waiting. When it was our number, we got up and entered the restaurant

1.3 noun mapping
Life NOUN RxJava nouns Effect
customer Observer Be responsible for monitoring events (monitoring call, taking different actions according to parameters, waiting or getting up)
Waiter Observed Responsible for generating events (call, parameter is number)
Call sign Signature Connecting the observer and the observed
Call number Event The carrier of communication between the observer and the observed
1.4 in a word

The Observable sends events to the Observer in order by signing or subscribing, and the Observer receives events in order & & and makes corresponding response actions.

2 learn to use

2.1 basic usage
package liujinliang.natappvip.cc.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the observed
        Observable<Integer> observable = Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> observableEmitter) throws Exception {
                observableEmitter.onNext(1);
                observableEmitter.onComplete();
            }
        });
        // Create observer
        Observer<Integer> observer = new Observer<Integer>() {

            @Override
            public void onSubscribe(Disposable disposable) {
                Log.i("Gene", "onSubscribe");
            }

            @Override
            public void onNext(Integer integer) {
                Log.i("Gene", "onNext:" + integer);
            }

            @Override
            public void onError(Throwable throwable) {
                Log.i("Gene", "onError");
            }

            @Override
            public void onComplete() {
                Log.i("Gene", "onComplete");
            }
        };

        // Related observer and observed
        observable.subscribe(observer);
    }
}
2.2 chain call
package liujinliang.natappvip.cc.myapplication;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Create the observed
        Observable.create(new ObservableOnSubscribe<Integer>() {
            @Override
            public void subscribe(ObservableEmitter<Integer> observableEmitter) throws Exception {
                observableEmitter.onNext(1);
                observableEmitter.onComplete();
            }
        }).subscribe(new Observer<Integer>() {

            @Override
            public void onSubscribe(Disposable disposable) {
                Log.i("Gene", "onSubscribe");
            }

            @Override
            public void onNext(Integer integer) {
                Log.i("Gene", "onNext:" + integer);
            }

            @Override
            public void onError(Throwable throwable) {
                Log.i("Gene", "onError");
            }

            @Override
            public void onComplete() {
                Log.i("Gene", "onComplete");
            }
        });
    }
}

Topics: Android