android - RxJavar用什么操作符可以使数据每隔一段时间取出一个
PHP中文网
PHP中文网 2017-04-17 17:52:23
0
2
613

RxJavar用什么操作符可以使数据每隔一段时间取出一个

PHP中文网
PHP中文网

认证0级讲师

reply all(2)
大家讲道理

I’m too lazy to work on the Java environment. I’ll give you a JavaScript example. You can follow it and change it to Java

const Rx = require("rx");

Rx.Observable.range(0, 10)
    .map(n => 3 + n * 10)
    .concatMap((x, i) => {
        return Rx.Observable.interval(500)
            .take(1)
            .map(() => `${x}:${i}`);
    })
    .do(console.log)
    .subscribe();

Just to add

interval(500).take(1) can be replaced by timer(500) interval(500).take(1) 可以用 timer(500) 代替

使用 concatMap() 或者 map().concat()

Use concatMap() or map().concat()

]🎜
Peter_Zhu
private Object getData(int index) {
    //TODO 获取第n个数据
}


Observable.interval(1, TimeUnit.SECONDS)//每秒执行一次
          .flatMap(i->Observable.fromCallable(()->getData(i.intValue())))//获取数据
          .subscribe(s->System.out.println(s));//获取数据后的处理方法

If the method of obtaining the object is a fast execution method, flatMap can also be replaced by map and changed to

          .map(i->getData(i.intValue()))//获取数据
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template