0
0

More than 1 year has passed since last update.

RxJava3でデバッグ時によく使う機能

Posted at

初めに

今回はRxJava3を使った開発でとく使われる関数を紹介していこうと思います

本文

error
こちらは、文字通りわざとエラーを吐かせたい時に使います。
下記が実際にエラーを吐かせるときの例です

override fun api(): Single<Hoge> {
    return Single.error(createHttpException(500))
}
private fun createHttpException(errorCode: Int) = HttpException(
    Response.error<Hoge>(
        errorCode, "".toResponseBody("plain/text".toMediaTypeOrNull())
    )
)

just
次に紹介するのが、返却物に仮のデータを入れることができる時に使うjustです。
これを使うと手元で欲しいデータを作ることができるので、わざわざその通信のMockを作る必要がなくなります。
下記が例です

override fun api(): Single<Hoges> {
    return Single.just(
        Hoges(
            emptyList()
        )
    )
}

最後に

自分はcoroutineを今までメインで触ってきたので、最近触ったRxJava3の便利な機能をまとめてみました。
何かのお役に立てれば幸いです

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0