R. *_*ski 11 java mvp multithreading android rx-java
我试图调用一个返回void(Java原始类型)的方法.我想延迟调用一个预定义的毫秒数.我知道这可以简单地使用Handler我喜欢不使用它.
我试着这样做:
Observable.just(getView().setAttachments(attachments)).delay(50, TimeUnit.MILLISECONDS);
Run Code Online (Sandbox Code Playgroud)
但是,有一个编译错误,即:
Observable.just(java.lang.Void) cannot be applied to (void)
Run Code Online (Sandbox Code Playgroud)
还有另外一种方法吗?我不想使用的原因Handler是代码是在Presenter(MVP模式)中定义的,我不想在仅Java类中使用Android特定代码.我更喜欢它是一个冷的Observable,因为我不必订阅它,只调用一次方法.
Mat*_*Bos 33
Observable.defer(() -> Observable.just(null)
.delay(3, TimeUnit.SECONDS))
.doOnNext(ignore -> LogUtils.d("You action goes here"))
.subscribe();
Run Code Online (Sandbox Code Playgroud)
在RxJava 2中,您可以使用以下内容:
Completable.complete()
.delay(3, TimeUnit.SECONDS)
.doOnComplete(() -> System.out.println("Time to complete " + (System.currentTimeMillis() - start)))
.subscribe();
Run Code Online (Sandbox Code Playgroud)
Paul的版本测试代码:
@Test
public void testTimeToDoOnSubscribeExecution() {
final long startTime = System.currentTimeMillis();
System.out.println("Starting at: " + startTime);
Subscription subscription = Observable.empty()
.doOnSubscribe(() -> System.out.println("Time to invoke onSubscribe: " + (System.currentTimeMillis() - startTime)))
.delay(1000, TimeUnit.MILLISECONDS)
.subscribe();
new TestSubscriber((rx.Observer) subscription).awaitTerminalEvent(2, TimeUnit.SECONDS);
}
Run Code Online (Sandbox Code Playgroud)
输出:
Starting at: 1467280697232
Time to invoke onSubscribe: 122
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6026 次 |
| 最近记录: |