Observable.from() does not work

和自甴很熟 提交于 2019-12-12 04:06:37

问题


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);
}

}

来源:https://stackoverflow.com/questions/46322943/observable-from-does-not-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!