RxJavar用什么操作符可以使数据每隔一段时间取出一个
认证0级讲师
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) 代替
interval(500).take(1)
timer(500)
使用 concatMap() 或者 map().concat()
concatMap()
map().concat()
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()))//获取数据
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
Just to add
interval(500).take(1)
can be replaced bytimer(500)
interval(500).take(1)
可以用timer(500)
代替使用
UseconcatMap()
或者map().concat()
concatMap()
ormap().concat()
If the method of obtaining the object is a fast execution method, flatMap can also be replaced by map and changed to