Is out there any Retrofit CallAdapter that returns...
# android
p
Is out there any Retrofit CallAdapter that returns
Flow<T>
. Is it a bad design?
j
Why do you want this over
suspend fun
?
☝️ 5
p
Just for symmetry with Rx. I am thinking on Rx1 migration to Flow. I am most likely conceptually wrong trying to equate Observable and Flow.
j
They are equivalent, but
Observable
is also somewhat incorrect for representing a network request. RxJava 2 fixed this with the
Single
type.
p
for a network request thats right.. However, in other scenarios some of my streams emit more than one event, like
State.Loading
,
State.Data
. What I normally do is having an Interactor/Actor chaining the Observables. I would like to do the same with Flow.
suspend
works is just that the chain does not look the same. My knowledge on coroutines is entry level, there is probably a better way.
j
Sure but Retrofit models only the network request part. You can convert
Single
to
Observable
where necessary and
suspend fun
integrates into
Flow
operators as well.
p
and
suspend fun
integrates into
Flow
operators as well
Definitely a good approach, I haven’t tried. Thanks!
g
You even can easily convert suspend function to flow directly (like Single -> Observable), so it will be lazy, if you want:
Copy code
::someFunction.asFlow()
p
Thanks for replying. You mean ::someSuspendFunction.asFlow right? I still have a question either calling the
retrofitApi.suspendingFunc()
from an enclosing Flow or
retrofitApi::suspendingFunc().asFlow()
. If I want to propagate a custom CustomHttpException or CustomIOException down the stream. Should I enclose the retrofit api suspended call in a try/catch block, or append a .catch() operator after the Flow? With Observables I use .onErrorReturn() operator to intercept erros.
g
You mean ::someSuspendFunction.asFlow right?
It works for suspend and common functions
p
I just saw another scenario to convert to Flow, is when refactoring a zip operator. Is easier to wrap every retrofit api call in a Flow and keep the same code just replacing Observable.zip by Flow.zip than dealing with suspend functions+asyncBuilder+await+exception
g
retrofitApi.suspendFunc().asFlow() is incorrect, it should be
retrofitApi::suspendFunc.asFlow()
or
suspend { retrofitApi.suspendFunc(param) }.asFlow()
If you just want to migrate existing Retrofit service with Observable, Flow is fine, but refactor code to use suspend functions for Network requests is much more clear and less error prone in most cases
👍 1
p
Oh ok, thanks for the clarification on
.asFlow()
usage. I just corrected my post so it does not confuse any reader. I see the point, I will move into that direction then.
g
retrofitApi::suspendingFunc().asFlow()
is still not correct, goal here is to convert method reference to flow, so when terminal operator will be called this method reference will be invoked by Flow chain