简体   繁体   English

为什么我们需要Observer.from()

[英]Why do we need Observer.from()

I am learning RxJava in Android. 我正在学习Android中的RxJava。 I am following this tutorial here: http://plavatvornica.com/rxjava-for-beginners/ 我在这里关注本教程: http : //plavatvornica.com/rxjava-for-beginners/

as shown in the code below,i created an observable that emits the items in a list one by onError 如下代码所示,我创建了一个可观察对象,该对象通过onError发出列表中的项目

I have a question to deepn my understaning of the usage of RxJava libarary. 我有一个问题需要加深我对RxJava库的使用的了解。 the questio is, why do I need to use RxJava to gt items from a list one by one? 问题是,为什么我需要使用RxJava来逐个列出列表中的项目? i could do the same think using a loop!! 我可以做同样的想法使用循环! since RxJava library offers such feature,I believe it should have ensued due to a specific need. 由于RxJava库提供了这样的功能,因此我相信由于特定的需要而应该随之而来。

can you please help me to understand it 你能帮我理解吗

code

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

    @Override
    public void onError(Throwable e) {
        Log.e(TAG, e.getMessage().toString());
    }

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

    }
};

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

    myObservable3.subscribe(listSubscriber);

It comes from the idea of Reactive Programming: nothing is hopping until you sent a message, ie in the real application the data will never be requested until you subscribe to the observer. 它来自于响应式编程的思想:在您发送一条消息之前,什么都不会跳动,即在实际的应用程序中,只有订阅观察者后,才需要请求数据。 Your example, particularly, doesn't express this idea. 特别是您的示例并没有表达这个想法。 But, if it would be, for example, database fetching, then it would be helpful. 但是,例如,如果这是数据库获取,那么它将很有帮助。 Moreover, reactive programming especially nice when async programming, because allows process data by part directly it was received without idle. 此外,当进行异步编程时,反应式编程特别好,因为它允许直接部分接收过程数据,而没有空闲。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM