Mohamed Fathy
08/28/2022, 8:04 AMfun <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 AMFlow<Any>
instead Flow<T>
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:
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?luke_c
08/28/2022, 9:56 AMMohamed Fathy
08/28/2022, 10:00 AMluke_c
08/28/2022, 10:05 AMMohamed Fathy
08/28/2022, 10:08 AM