LoginSignup
6
2

More than 3 years have passed since last update.

Retrofit+CoroutineのJake神ライブラリが非推奨になっていた

Last updated at Posted at 2019-08-25

今までRetrofit+Coroutineで通信処理を書く際には、jake神が作ってくださったライブラリを使っていた方が多いと思います。

https://github.com/JakeWharton/retrofit2-kotlin-coroutines-adapter

非推奨

すでに非推奨になって3ヶ月ほど経っていました。
なので、自分が知らなかっただけで既にQitiaの記事にあるかもしれません。。。。

非推奨の旨がReadmeに書いてあって

Kotlin Coroutine Adapter (DEPRECATED)
A Retrofit 2 CallAdapter.Factory for Kotlin coroutine's Deferred.

This library is deprecated. Please migrate to Retrofit 2.6.0 or newer and its > > built-in suspend support

バージョン2.6.0以降のRetrofit側で対応したので、
そちらの機能を使うように案内があります。

それを使うことによって、今までの

@GET("users/{id}")
fun user(@Path("id") id: Long): Deferred<User>

から

@GET("users/{id}")
suspend fun user(@Path("id") id: Long): User

とsuspend関数を使う形になります。

理由?

RetrofitのKotlin Coroutine Adapterにひそむ罠

https://qiita.com/kafumi/items/e8b9a74b59e00d5e2f3a

上の記事で知ったのですが、jake神のライブラリはsuspend関数でないものに、無理してcoroutine対応を入れていたため、通信を行なっているCoroutineScopeが、呼び出し元と親子関係のないものになっていたそうです。
そのため、通信処理でエラーが発生した場合に、正しくエラーを伝播できない問題がありました。(本人がissueで匙を投げていた模様)

なので特に理由がない場合、早めに置き換えた方が良いと思います。

6
2
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
6
2