0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

RxJavaのdoAfterNextを使う

0
Posted at

RxJavaで一つ前の川で受信用のobservableをflatmapとかで設定して、その直後で送信処理するみたいな事書きたい

こういう事がしたかった。

doAfterNextを使うと解決出来そうだった。サンプルコードはこちら
publishSubject.onNextのとこらへんで送信開始処理みたいなのを行うイメージ

        val publishSubject = PublishSubject.create<Int>()

        Observable.just(1)
                .doAfterNext {
                    println("doAfterNext")
                    publishSubject.onNext(it * 2)
                }
                .flatMap {
                    println("receive start")
                    publishSubject
                }
                .subscribe({ println(it) })

出力結果はこちら

receive start
doAfterNext
2
  1. flatMapでpublishSubjectが設定される
  2. その後doAfterNextが呼ばれて、publishSubjectで1 * 2が流される
  3. subscribeしてるところで2がとれる

という事で無事に出来ました。もうちょっと早く気がついてたらもっといい川を書けてたかもしれない。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?