Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RxJava1からRxJava2へ

 RxJava1からRxJava2へ

Yasuhiro Shimizu

October 03, 2017
Tweet

More Decks by Yasuhiro Shimizu

Other Decks in Programming

Transcript

  1. ABOUT ME 清水泰博(Yasuhiro Shimizu) ▸ Android Engineer @AWA Co. Ltd.

    ▸ GitHub: yshrsmz ▸ Twitter: _yshrsmz Omnitweety Monotweety
  2. MIGRATING TO RXJAVA2 What is RxJava2Interop? ▸ https://github.com/akarnokd/RxJava2Interop ▸ RxJava1とRxJava2のReactive

    Typeを相互に変換 ▸ 作: David Karnok(RxJavaのmain commiter) ▸ このライブラリを使うとステップ・バイ・ステップで RxJava2に移行できる
  3. MIGRATING TO RXJAVA2 Usage // app/build.gradle android.packagingOptions { exclude 'META-INF/rxjava.properties'

    } dependencies { compile: "io.reactivex:rxjava:1.3.2" compile: "io.reactivex.rxjava2:rxjava:2.1.4" compile: "com.github.akarnokd:rxjava2-interop:0.10.6 }
  4. MIGRATING TO RXJAVA2 使い方(in Kotlin) fun <T> rx.Observable<T>.toV2Observable() = RxJavaInterop.toV2Observable(this)

    fun <T> io.reactivex.Observable<T>.toV1Observable() = RxJavaInterop.toV1Observable(this, BackpressureStrategy.LATEST) // v1Observable.toV2Observable() // v2Observable.toV1Observable() 拡張関数を定義すると捗る
  5. MIGRATING TO RXJAVA2 Realm ▸ Realm v4.0はRxJava2をサポートする(未リリース) ▸ v3.xでは2つ選択肢がある ▸

    RxJava2Interopを使う ▸ v4.0の実装を参考に自分で作る ▸ https://github.com/realm/realm-java/pull/4991 ▸ Kotlinの拡張関数使うと
  6. MITRATING TO RXJAVA2 Retrofit ▸ RxJava2CallAdapterFactoryを使う ▸ RxJava1とRxJava2のCallAdapterは共存できる Retrofit.Builder() .baseUrl("https://api.example.com")

    .addCallAdapterFactory( RxJavaCallAdapterFactory.create()) .addCallAdapterFactory( RxJava2CallAdapterFactory.create()) .build()
  7. MIGRATING TO RXJAVA2 Architecture of AWA DbClient DbClient DbClient DbClient

    DbClient ApiClient SharedPreferences Realm SQLite API Server DbClient DbClient Model DbClient DbClient UseCase DBCLIENT DBCLIENT Activity/Fragment View Domain Data
  8. MIGRATING TO RXJAVA2 Architecture of AWA DbClient DbClient DbClient DbClient

    DbClient ApiClient SharedPreferences Realm SQLite API Server DbClient DbClient Model DbClient DbClient UseCase DBCLIENT DBCLIENT Activity/Fragment View Domain Data DbClientをRxJava2化 RxJava2Interop でRxJava1に変換 Domainより上の レイヤは変更不要
  9. MIGRATING TO RXJAVA2 Some random tips ▸ doOnDisposeではなくdoFinallyを使う ▸ https://github.com/ReactiveX/RxJava/wiki/What's-

    different-in-2.0 ▸ Reactive Typeで「値がない状態」を通知したい時はOptionalを 使う ▸ Kotlinだとgojuno/koptionalがミニマムでよさそう ▸ Singleで↑の場合はMaybeを使いましょう