public class ReactiveExample {
public static void main(String[] args) throws InterruptedException, NumberFormatException, UnsupportedEncodingException {
Observable.range(1, 5).subscribe(
System.out::println,
error -> System.out.println("error"),
() -> System.out.println("completed")
);
}
}打印输出的结果是rx.internal.util.ScalarSynchronousObservable@2fad386b
在打印了可观察性之后,我得到:
Process finished with exit code 0。
我刚开始使用ReactiveX,并且一直在关注一些教程。我的假设是上面的代码会像这样连续流式传输数据:
1-2-3-4-5-1-2-3-4-5...并继续打印值。为什么我的程序会立即停止?它是在观察到前5位数字后结束的吗?如何才能将其更改为连续流式传输值并在这些值循环时打印它们?另外,我如何实际打印值而不是observable对象引用?
发布于 2017-06-04 03:23:22
range: Returns an Observable that emits a sequence of Integers within a specified range.
所以你的假设是错误的。range不会重复任何内容。为此,您需要使用repeat
Observable.range(1, 5).repeat().subscribe(
System.out::println,
error -> System.out.println("error"),
() -> System.out.println("completed")
);https://stackoverflow.com/questions/43468454
复制相似问题