Hi guys, I am very new to Kotlin coroutines and Fl...
# android
m
Hi guys, I am very new to Kotlin coroutines and Flow and studying it now, I tried to achieve this behavior in Flow, but I am not able to do it unfortunately
Copy code
fun <T> Single<Response<T>>.parseResponse(): Single<T> {
  return flatMap { response ->
    if (response.isSuccessful) {
      if (response.body() != null) Single.just(response.body()) else Single.error(NoDataException())
    } else {
      Single.error(DataRetrievingFailException())
    }
  }
}
This is extension written with RxJava that takes
Single
of Retrofit
Response<Model>
(
Single<Response<Model>>
) and check the state of it and then return
Single<Model>
or error, This function supposed to be used in the
data
layer. Thanks in advance.
Tried to do this but I get error because it now returns
Flow<Any>
instead
Flow<T>
Copy code
fun <T> Flow<Response<T>>.parseResponse(): Flow<T> {
  return map { response ->
    if (response.isSuccessful) {
      if (response.body() != null) response.body() else NoDataException()
    } else {
      DataRetrievingFailException()
    }
  }
}
Also tried this one and same return type error:
Copy code
fun <T> Flow<Response<T>>.parseResponse(): Flow<T> {
  return flatMapConcat { response ->
    if (response.isSuccessful) {
      if (response.body() != null) flowOf(response.body()) else catch { emit(NoDataException()) }
    } else {
      catch { emit(DataRetrievingFailException()) }
    }
  }
}
Is it hard to achieve the same behaviour with flow or what?
l
A regular suspend function would be a better replacement for a Single
m
Can you please show example?
m
Thanks a lot