-->

Observable.from() does not work

2019-08-25 09:23发布

问题:

I am learning RxJava in Android. I am following this tutorial here:

as shown in the code below, i have three observables and one subscriber. at run time, myObservable2 and myObservable2 are executed but not myObservable3.

to find out why myObservable3 s not executed, I commeted out myObservable2 and myObservable2 and ran only myObservable3 and then it ran normally.

why I can not run myObservable2 and myObservable2 and myObservable3 at the same time.

code

public class MainActivity extends AppCompatActivity {

private static final String TAG = MainActivity.class.getSimpleName();

Observable myObservable1 = Observable.create(new Observable.OnSubscribe<String>() {

    //this method cant be overriden
    @Override
    public void call(Subscriber<? super String> subscriber) {
        subscriber.onNext("first observer");
        subscriber.onCompleted();
    }
});

Subscriber<String> mySubscriber = new Subscriber<String>() {
    @Override
    public void onCompleted() {
        Log.i(TAG, "I am done");
    }

    @Override
    public void onError(Throwable e) {

    }

    @Override
    public void onNext(String s) {
        Log.i(TAG, "onNext: " + s);
    }
};

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

    //can use the same subscriber
    Observable myObservable2 = Observable.just("second observer");

    //list as observable
    List<String> list = new ArrayList<>();
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    Observable myObservable3 = Observable.from(list);

    //subscription
    myObservable1.subscribe(mySubscriber);
    myObservable2.subscribe(mySubscriber);
    myObservable3.subscribe(mySubscriber);
}

}