repeatUtil() 不适用于 Rxjava 2.x
The repeatUtil() not work on Rxjava 2.x
当我使用下面的代码时:(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 不会 executed.The 控制台不会记录任何内容。
但是如果我使用下面的代码:(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 将是 executed.And 控制台将记录:
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()方法?
它需要一个 emitter.onComplete()
否则不会触发重复。
当我使用下面的代码时:(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 不会 executed.The 控制台不会记录任何内容。
但是如果我使用下面的代码:(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 将是 executed.And 控制台将记录:
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()方法?
它需要一个 emitter.onComplete()
否则不会触发重复。