io.reactivex.Observable.doFinally()方法的使用及代码示例

x33g5p2x  于2022-01-25 转载在 其他  
字(5.6k)|赞(0)|评价(0)|浏览(183)

本文整理了Java中io.reactivex.Observable.doFinally()方法的一些代码示例,展示了Observable.doFinally()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Observable.doFinally()方法的具体详情如下:
包路径:io.reactivex.Observable
类名称:Observable
方法名:doFinally

Observable.doFinally介绍

[英]Calls the specified action after this Observable signals onError or onCompleted or gets disposed by the downstream.

In case of a race between a terminal event and a dispose call, the provided onFinally action is executed once per subscription.

Note that the onFinally action is shared between subscriptions and as such should be thread-safe.

Scheduler: doFinally does not operate by default on a particular Scheduler. Operator-fusion: This operator supports boundary-limited synchronous or asynchronous queue-fusion.

History: 2.0.1 - experimental
[中]在这个可观察到的信号发出onError或onCompleted或被下游处理后调用指定的操作。
如果终端事件和dispose调用之间存在竞争,则提供的onFinally操作将在每个订阅中执行一次。
请注意,onFinally操作在订阅之间共享,因此应该是线程安全的。
调度器:doFinally默认情况下不会在特定的调度器上运行。运算符融合:该运算符支持边界受限的同步或异步队列融合。
历史:2.0.1-实验性

代码示例

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<Object> apply(Observable<Object> f) throws Exception {
    return f.doFinally(ObservableDoFinallyTest.this);
  }
});

代码示例来源:origin: ReactiveX/RxJava

@Test(expected = NullPointerException.class)
public void nullAction() {
  Observable.just(1).doFinally(null);
}

代码示例来源:origin: ReactiveX/RxJava

@Override
  public Observable<Object> apply(Observable<Object> f) throws Exception {
    return f.doFinally(ObservableDoFinallyTest.this).filter(Functions.alwaysTrue());
  }
});

代码示例来源:origin: ReactiveX/RxJava

@Test
public void normalJust() {
  Observable.just(1)
  .doFinally(this)
  .test()
  .assertResult(1);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void clearIsEmpty() {
  Observable.range(1, 5)
  .doFinally(this)
  .subscribe(new Observer<Integer>() {

代码示例来源:origin: ReactiveX/RxJava

@Test
public void normalJustConditional() {
  Observable.just(1)
  .doFinally(this)
  .filter(Functions.alwaysTrue())
  .test()
  .assertResult(1);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void syncFusedBoundary() {
  TestObserver<Integer> to = ObserverFusion.newTest(QueueFuseable.SYNC | QueueFuseable.BOUNDARY);
  Observable.range(1, 5)
  .doFinally(this)
  .subscribe(to);
  ObserverFusion.assertFusion(to, QueueFuseable.NONE)
  .assertResult(1, 2, 3, 4, 5);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void syncFused() {
  TestObserver<Integer> to = ObserverFusion.newTest(QueueFuseable.SYNC);
  Observable.range(1, 5)
  .doFinally(this)
  .subscribe(to);
  ObserverFusion.assertFusion(to, QueueFuseable.SYNC)
  .assertResult(1, 2, 3, 4, 5);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void normalEmpty() {
  Observable.empty()
  .doFinally(this)
  .test()
  .assertResult();
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void nonFused() {
  TestObserver<Integer> to = ObserverFusion.newTest(QueueFuseable.SYNC);
  Observable.range(1, 5).hide()
  .doFinally(this)
  .subscribe(to);
  ObserverFusion.assertFusion(to, QueueFuseable.NONE)
  .assertResult(1, 2, 3, 4, 5);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void syncFusedConditional() {
  TestObserver<Integer> to = ObserverFusion.newTest(QueueFuseable.SYNC);
  Observable.range(1, 5)
  .doFinally(this)
  .filter(Functions.alwaysTrue())
  .subscribe(to);
  ObserverFusion.assertFusion(to, QueueFuseable.SYNC)
  .assertResult(1, 2, 3, 4, 5);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void syncFusedBoundaryConditional() {
  TestObserver<Integer> to = ObserverFusion.newTest(QueueFuseable.SYNC | QueueFuseable.BOUNDARY);
  Observable.range(1, 5)
  .doFinally(this)
  .filter(Functions.alwaysTrue())
  .subscribe(to);
  ObserverFusion.assertFusion(to, QueueFuseable.NONE)
  .assertResult(1, 2, 3, 4, 5);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void normalTake() {
  Observable.range(1, 10)
  .doFinally(this)
  .take(5)
  .test()
  .assertResult(1, 2, 3, 4, 5);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void normalError() {
  Observable.error(new TestException())
  .doFinally(this)
  .test()
  .assertFailure(TestException.class);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void nonFusedConditional() {
  TestObserver<Integer> to = ObserverFusion.newTest(QueueFuseable.SYNC);
  Observable.range(1, 5).hide()
  .doFinally(this)
  .filter(Functions.alwaysTrue())
  .subscribe(to);
  ObserverFusion.assertFusion(to, QueueFuseable.NONE)
  .assertResult(1, 2, 3, 4, 5);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void actionThrows() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    Observable.just(1)
    .doFinally(new Action() {
      @Override
      public void run() throws Exception {
        throw new TestException();
      }
    })
    .test()
    .assertResult(1)
    .cancel();
    TestHelper.assertUndeliverable(errors, 0, TestException.class);
  } finally {
    RxJavaPlugins.reset();
  }
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void normalEmptyConditional() {
  Observable.empty()
  .doFinally(this)
  .filter(Functions.alwaysTrue())
  .test()
  .assertResult();
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void normalErrorConditional() {
  Observable.error(new TestException())
  .doFinally(this)
  .filter(Functions.alwaysTrue())
  .test()
  .assertFailure(TestException.class);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void normalTakeConditional() {
  Observable.range(1, 10)
  .doFinally(this)
  .filter(Functions.alwaysTrue())
  .take(5)
  .test()
  .assertResult(1, 2, 3, 4, 5);
  assertEquals(1, calls);
}

代码示例来源:origin: ReactiveX/RxJava

@Test
public void actionThrowsConditional() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    Observable.just(1)
    .doFinally(new Action() {
      @Override
      public void run() throws Exception {
        throw new TestException();
      }
    })
    .filter(Functions.alwaysTrue())
    .test()
    .assertResult(1)
    .cancel();
    TestHelper.assertUndeliverable(errors, 0, TestException.class);
  } finally {
    RxJavaPlugins.reset();
  }
}

相关文章

Observable类方法