Retrofit: [Feature Request] Support adapter for Kotlin Coroutine Flow

Created on 19 Nov 2020  路  2Comments  路  Source: square/retrofit

Feature Request

It would be great if it supports Kotlin Coroutine Flow<T> adapter for getting a response in the stream.

For example:

interface FeedApi {
    @GET("/")
    fun getPosts(): Flow<List<Post>>
}

If this is available then it will be easy to use and it'll be easy to map network response to the app's data model and for many Flow specific operations as well.

Most helpful comment

HTTP requests return a single response, not a stream of responses, and so only suspend fun is supported.

interface FeedApi {
    @GET("/")
    suspend fun getPosts(): List<Post>
}

If you need to emit this as a flow you can do something like

flow {
  emit(getPosts())
}

We won't be supporting Flow out of the box, and if we had the chance to do the RxJava adapters over again we probably wouldn't support their Observable either (Single is the natural type to use).

All 2 comments

HTTP requests return a single response, not a stream of responses, and so only suspend fun is supported.

interface FeedApi {
    @GET("/")
    suspend fun getPosts(): List<Post>
}

If you need to emit this as a flow you can do something like

flow {
  emit(getPosts())
}

We won't be supporting Flow out of the box, and if we had the chance to do the RxJava adapters over again we probably wouldn't support their Observable either (Single is the natural type to use).

Thanks!

Was this page helpful?
0 / 5 - 0 ratings