如何使用RxJava的repeatUntil运算符?

时间:2016-12-08 04:07:51

标签: android rx-java

我未能找到有关RxJava的repeatUntil运算符的文档。有人可以提供一个例子吗?

谢谢。

2 个答案:

答案 0 :(得分:1)

简单示例:

someObservable.repeatUntil(new BooleanSupplier() {
    @Override
    public boolean getAsBoolean() throws Exception {
        return false; //repeat when upstream Observable calls onComplete
        return true;  //don't repeat and go downstream
    }
});

官方RxJava javadoc是hereRepeatUntil说明为here

答案 1 :(得分:0)

每500ms两次打印连续数字的示例:

$ valgrind ./bin/strlcat C-Programming can-do
==15976== Memcheck, a memory error detector
==15976== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==15976== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==15976== Command: ./bin/strlcat C-Programming can-do
==15976==
src: C-Programming
dst: can-do
str: C-Programming can-do
len: 20 chars.
==15976==
==15976== HEAP SUMMARY:
==15976==     in use at exit: 0 bytes in 0 blocks
==15976==   total heap usage: 2 allocs, 2 frees, 36 bytes allocated
==15976==
==15976== All heap blocks were freed -- no leaks are possible
==15976==
==15976== For counts of detected and suppressed errors, rerun with: -v
==15976== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

输出:

public class Application {

    public static void main(String[] args) throws InterruptedException {

        final long startTimeMillis = System.currentTimeMillis();
        Observable.interval(500, TimeUnit.MILLISECONDS)
            .take(5) //takes the first five emissions from the observable source
            .repeatUntil(() -> System.currentTimeMillis() - startTimeMillis > 5000)
            .subscribe(System.out::println);

        Thread.sleep(6000);
    }
}