我有Observable
个发光物品,我想将自收到最后一个物品以来作为“时间刻度”的特殊物品合并。
我试图使用timeout
+ onErrorXXX
或interval
s,但无法按预期工作。
import io.reactivex.Observable;
import io.reactivex.functions.Function;
import org.apache.log4j.Logger;
import org.junit.Test;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
public class RXTest {
private static final Logger log = Logger.getLogger(RXTest.class);
@Test
public void rxTest() throws InterruptedException {
log.info("Starting");
Observable.range(0, 26)
.concatMap(
item -> Observable.just(item)
.delay(item, TimeUnit.SECONDS)
)
.timeout(100, TimeUnit.MILLISECONDS)
// .retry()
.onErrorResumeNext((Function) throwable -> {
if (throwable instanceof TimeoutException) {
return Observable.just(-1);
}
throw new RuntimeException((Throwable)throwable);
})
.subscribe(
item -> log.info("Received " + item),
throwable -> log.error("Thrown" + throwable),
() -> log.info("Completed")
);
Thread.sleep(30000);
}
}
我希望它能输出如下内容:
<00> 00:00.000收到0
00:00.100收到-1
00:00.200收到-1
...(每100毫升更多收到-1)
00:01.000收到1
00:01.100收到-1
00:01.200收到-1
...
00:03.000收到2
00:03.100收到-1
00:03.200收到-1
......
但相反,它只接收一次-1然后完成。
答案 0 :(得分:0)
我不清楚你想要什么,但如果你改变onResumeNext()
返回
Observable.interval(100, TimeUnit.MILLISECONDS)
.take(2)
.map(__ -> -1)