简体   繁体   English

可见不重复

[英]Observable not repeating

I'm trying to repeat() an action in an observable but it is not performing it more than once. 我试图在一个可观察到的动作中repeat()一个动作,但是它执行的次数不止一次。

Observable.create(new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(ObservableEmitter<String> e) throws Exception {
            // **** Code Here is not repeating ****
            e.onNext(pullMessagesFromServer());
        }
    })
    .repeat()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .map(new Function<String, JSONArray>() {
        @Override
        public JSONArray apply(String s) throws Exception {
            return JsonParser.parseString(s);
        }
    })
    .subscribe(new Observer<JSONArray>() {
        @Override
        public void onNext(JSONArray arr) {
            // **** Code Here is not repeating ****
        }  
    });
}

What gives? 是什么赋予了? Thanks! 谢谢! New to Rxjava and it's confusing me somewhat. Rxjava的新手,这使我有些困惑。 :( :(

From my comment: repeat operates if the source completes. 根据我的评论:如果源代码完整,则重复操作。 Add e.onComplete() . 添加e.onComplete()

Observable.create(new ObservableOnSubscribe<String>() {
        @Override
        public void subscribe(ObservableEmitter<String> e) throws Exception {
            // **** Code Here is not repeating ****
            e.onNext(pullMessagesFromServer());
            e.onComplete();
        }
    })
    .repeat()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .map(new Function<String, JSONArray>() {
        @Override
        public JSONArray apply(String s) throws Exception {
            return JsonParser.parseString(s);
        }
    })
    .subscribe(new Observer<JSONArray>() {
        @Override
        public void onNext(JSONArray arr) {
            // **** Code Here is not repeating ****
        }  
    });
}

The reason for this is that the Observable protocol is potentially asynchronous, that is, events may arrive over time. 这样做的原因是, Observable协议可能是异步的,也就是说,事件可能会随时间到达。 Therefore, just because your code stopped signalling events, the library can't know there won't be any further events. 因此,仅因为您的代码停止了发信号通知事件,库就不知道不会再有其他事件了。 You have to be explicit about indicating no more values will be emitted when using create() . 您必须明确指出使用create()时将不再发射任何值。 Generally, you can use other factory methods such as fromCallable that will make sure a single generated value will be properly signaled: 通常,您可以使用其他工厂方法,例如fromCallable ,以确保可以正确发出单个生成值的信号:

Observable.fromCallable(new Callable<String>() {
        @Override
        public String call() throws Exception {
            // **** Code Here is not repeating ****
            return pullMessagesFromServer();
        }
    })
    .repeat()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .map(new Function<String, JSONArray>() {
        @Override
        public JSONArray apply(String s) throws Exception {
            return JsonParser.parseString(s);
        }
    })
    .subscribe(new Observer<JSONArray>() {
        @Override
        public void onNext(JSONArray arr) {
            // **** Code Here is not repeating ****
        }  
    });
}

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

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