0

当我使用以下代码时:(Observable.create())

    Observable.create(new ObservableOnSubscribe<User>() {
        @Override
        public void subscribe(ObservableEmitter<User> emitter) throws Exception {
            emitter.onNext(new User("Even201314", 14));
        }
    }).repeatUntil(new BooleanSupplier() {
        @Override
        public boolean getAsBoolean() throws Exception {
            repeatCount += 1;
            Log.d(TAG, "count: " + repeatCount);
            return repeatCount > 10;
        }
    })

方法 repeatUntil 不会被执行。控制台不会记录任何东西。
但是如果我使用以下代码:(Observable.just())

    final User user = new User("Even201314", 24);
    Observable.just(user).repeatUntil(new BooleanSupplier() {
        @Override
        public boolean getAsBoolean() throws Exception {
            repeatCount += 1;
            Log.d(TAG, "count: " + repeatCount);
            return repeatCount > 10;
        }
    })

将执行 repeatUntil 方法。控制台将记录:

11-14 00:00:36.312 5788-5854/com.even.learningrxjava2 D/MainActivity: count: 1
11-14 00:00:36.312 5788-5854/com.even.learningrxjava2 D/MainActivity: count: 2
11-14 00:00:36.313 5788-5854/com.even.learningrxjava2 D/MainActivity: count: 3
11-14 00:00:36.313 5788-5854/com.even.learningrxjava2 D/MainActivity: count: 4
11-14 00:00:36.313 5788-5854/com.even.learningrxjava2 D/MainActivity: count: 5
...

我不知道为什么 Observable.create() 不会执行 repeatUntil() 方法?

4

1 回答 1

0

它需要一个emitter.onComplete()否则不会触发重复。

于 2016-11-14T08:19:15.320 回答