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

x33g5p2x  于2022-01-29 转载在 其他  
字(7.8k)|赞(0)|评价(0)|浏览(118)

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

Single.doFinally介绍

[英]Calls the specified action after this Single signals onSuccess or onError 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.

History: 2.0.1 - experimental
[中]在发出onSuccess或onError信号或被下游处理后调用指定的操作。
如果终端事件和dispose调用之间存在竞争,则提供的onFinally操作将在每个订阅中执行一次。
请注意,onFinally操作在订阅之间共享,因此应该是线程安全的。调度器:doFinally默认情况下不会在特定的调度器上运行。
历史:2.0.1-实验性

代码示例

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

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

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

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

代码示例来源:origin: igreenwood/SimpleCropView

/**
 * Crop image with RxJava2
 *
 * @param sourceUri Uri for cropping(If null, the Uri set in loadAsSingle() is used)
 *
 * @return Single of cropping image
 */
public Single<Bitmap> cropAsSingle(final Uri sourceUri) {
 return Single.fromCallable(new Callable<Bitmap>() {
  @Override public Bitmap call() throws Exception {
   if (sourceUri != null) mSourceUri = sourceUri;
   return cropImage();
  }
 }).doOnSubscribe(new Consumer<Disposable>() {
  @Override public void accept(@NonNull Disposable disposable) throws Exception {
   mIsCropping.set(true);
  }
 }).doFinally(new Action() {
  @Override public void run() throws Exception {
   mIsCropping.set(false);
  }
 });
}

代码示例来源:origin: igreenwood/SimpleCropView

/**
 * Save image with RxJava2
 *
 * @param bitmap Bitmap for saving
 * @param saveUri Uri for saving the cropped image
 *
 * @return Single of saving image
 */
public Single<Uri> saveAsSingle(final Bitmap bitmap, final Uri saveUri) {
 return Single.fromCallable(new Callable<Uri>() {
  @Override public Uri call() throws Exception {
   return saveImage(bitmap, saveUri);
  }
 }).doOnSubscribe(new Consumer<Disposable>() {
  @Override public void accept(@NonNull Disposable disposable) throws Exception {
   mIsSaving.set(true);
  }
 }).doFinally(new Action() {
  @Override public void run() throws Exception {
   mIsSaving.set(false);
  }
 });
}

代码示例来源:origin: TeamNewPipe/NewPipe

private void onBookmarkClicked() {
  if (isBookmarkButtonReady == null || !isBookmarkButtonReady.get() ||
      remotePlaylistManager == null)
    return;
  final Disposable action;
  if (currentInfo != null && playlistEntity == null) {
    action = remotePlaylistManager.onBookmark(currentInfo)
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(ignored -> {/* Do nothing */}, this::onError);
  } else if (playlistEntity != null) {
    action = remotePlaylistManager.deletePlaylist(playlistEntity.getUid())
        .observeOn(AndroidSchedulers.mainThread())
        .doFinally(() -> playlistEntity = null)
        .subscribe(ignored -> {/* Do nothing */}, this::onError);
  } else {
    action = Disposables.empty();
  }
  disposables.add(action);
}

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

@Test
  public void disposed() {
    TestHelper.checkDisposed(PublishSubject.create().singleOrError().doFinally(this));
  }
}

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

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

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

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

代码示例来源:origin: vert-x3/vertx-examples

.doFinally(conn::close)
).subscribe(resultSet -> {

代码示例来源:origin: igreenwood/SimpleCropView

.doFinally(new Action() {
 @Override public void run() throws Exception {
  dismissProgress();

代码示例来源:origin: Polidea/RxAndroidBle

@Override
protected void protectedRun(final ObservableEmitter<BluetoothGatt> emitter, final QueueReleaseInterface queueReleaseInterface) {
  final Action queueReleaseAction = new Action() {
    @Override
    public void run() {
      queueReleaseInterface.release();
    }
  };
  final DisposableSingleObserver<BluetoothGatt> disposableGattObserver = getConnectedBluetoothGatt()
      .compose(wrapWithTimeoutWhenNotAutoconnecting())
      // when there are no subscribers there is no point of continuing work -> next will be disconnect operation
      .doFinally(queueReleaseAction)
      .subscribeWith(disposableSingleObserverFromEmitter(emitter));
  emitter.setDisposable(disposableGattObserver);
  if (autoConnect) {
    // with autoConnect the connection may be established after a really long time
    queueReleaseInterface.release();
  }
}

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

@Test
public void actionThrows() {
  List<Throwable> errors = TestHelper.trackPluginErrors();
  try {
    Single.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: chat-sdk/chat-sdk-android

.doFinally(() -> progressBar.setVisibility(View.INVISIBLE))
.subscribe(bitmap -> {
  imageView.setImageBitmap(bitmap);

代码示例来源:origin: chat-sdk/chat-sdk-android

public void startChat () {
  if (startingChat) {
    return;
  }
  startingChat = true;
  showProgressDialog(getString(R.string.creating_thread));
  disposableList.add(ChatSDK.thread().createThread("", user, ChatSDK.currentUser())
      .observeOn(AndroidSchedulers.mainThread())
      .doFinally(() -> {
        dismissProgressDialog();
        startingChat = false;
      })
      .subscribe(thread -> {
        ChatSDK.ui().startChatActivityForID(getApplicationContext(), thread.getEntityID());
      }, throwable -> {
        ToastHelper.show(getApplicationContext(), throwable.getLocalizedMessage());
      }));
}

代码示例来源:origin: redhat-developer/introduction-to-eclipse-vertx

private Single<List<Article>> query(SQLConnection connection) {
  return connection.rxQuery("SELECT * FROM articles")
    .map(rs -> rs.getRows().stream().map(Article::new).collect(Collectors.toList()))
    .doFinally(connection::close);
}

代码示例来源:origin: avluis/Hentoid

private void checkForUpdates() {
  notificationManager.startForeground(new UpdateCheckNotification());
  disposable = UpdateServer.API.getUpdateInfo()
      .retry(3)
      .observeOn(mainThread())
      .doFinally(this::stopSelf)
      .subscribe(this::onCheckSuccess, this::onCheckError);
}

代码示例来源:origin: xiancloud/xian

@Override
  public String load(String tableName) {
    LOG.info("Load/refresh id column of table: " + tableName);
    return PoolFactory.getPool().getSlaveDatasource()
        .getConnection()
        .flatMap(connection -> BaseSqlDriver.XIAN_SQL_DRIVER_CLASS.newInstance()
            .setConnection(connection)
            .getIdCol(tableName)
            .doFinally(() -> connection.close().subscribe()))
        .blockingGet();
  }
});

代码示例来源:origin: info.xiancloud/xian-daocore

@Override
  public String load(String tableName) {
    LOG.info("Load/refresh id column of table: " + tableName);
    return PoolFactory.getPool().getSlaveDatasource()
        .getConnection()
        .flatMap(connection -> BaseSqlDriver.XIAN_SQL_DRIVER_CLASS.newInstance()
            .setConnection(connection)
            .getIdCol(tableName)
            .doFinally(() -> connection.close().subscribe()))
        .blockingGet();
  }
});

代码示例来源:origin: redhat-developer/introduction-to-eclipse-vertx

private Single<Article> insert(SQLConnection connection, Article article, boolean closeConnection) {
  String sql = "INSERT INTO Articles (title, url) VALUES (?, ?)";
  return connection
    .rxUpdateWithParams(sql, new JsonArray().add(article.getTitle()).add(article.getUrl()))
    .map(res -> new Article(res.getKeys().getLong(0), article.getTitle(), article.getUrl()))
    .doFinally(() -> {
      if (closeConnection) {
        connection.close();
      }
    });
}

代码示例来源:origin: io.vertx/vertx-rx-java2

private Single<List<String>> inTransaction(Exception e) throws Exception {
  return client.rxGetConnection().flatMap(conn -> {
   return rxInsertExtraFolks(conn)
    .andThen(uniqueNames(conn))
    .<List<String>>collect(ArrayList::new, List::add)
    .compose(upstream -> e == null ? upstream : upstream.flatMap(names -> Single.error(e)))
    .compose(SQLClientHelper.txSingleTransformer(conn))
    .flatMap(names -> rxAssertAutoCommit(conn).andThen(Single.just(names)))
    .doFinally(conn::close);
  });
 }
}

相关文章

微信公众号

最新文章

更多