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.
Mohamed Fathy
08/28/2022, 8:18 AM
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()
}
}
}