Can anyone shed some light on how you use coroutin...
# coroutines
f
Can anyone shed some light on how you use coroutines in addition to RxJava? Specifically how you use suspending functions instead of Single/Maybe/Completable? I understand that for one-offs that works nicely, but what if you want to use that as part of your stream? Thanks
g
In general I’m not sure that understand your question, suspend functions perfectly replace Single/Maybe/Completable For streams you need something different, like just released Flow or Channels
t
Conceptually, you can replace : -
Single
by suspend functions that return a non-null object, -
Maybe
by suspend functions that return a nullable object, -
Completable
by suspend functions that return
Unit
. Since yesterday, you can play with the preview of the new
Flow
API, that is intended to replace usages of
Observable
and
Flowable
in the long run.
If you have an existing Rx chain that needs to call suspending functions, you can call suspend functions by wrapping them in `runBlocking`:
Copy code
mySingle.map { element ->
    runBlocking {
        suspendingTransform(element)
    }
}
f
Thanks for the input. I use both the rx types and suspending functions independently. I was more curious of how people who use rx chains interop with suspending functions. Do you prefer to map using runBlocking or still rely on completable/maybe/single. As far as I can see you're losing the error handling if you do that?
g
Depends on case, mostly use Rx coroutines adapter
because blocking is actually bad strategy in many cases, you loose cancellation and async Maybe you have particular example where you would like to discuss how combine Observable with suspend function
As far as I can see you’re losing the error handling if you do that
No you do not loose actually, in this case if
suspendingTransform
will throw exception, map will catch it and propagate downstream, very similar to what you have with
flatMap
s
I'll be giving a talk about exactly this question, @Fredrik Larsen, at DroidCon Boston next Tuesday! :)
f
Thank you for the input! @gildor @streetsofboston cool! I hope it'll be available online? 😉
s
It will be available after the conference.
f
That's great 🙂 Good luck. I'm looking forward to seeing it.
s
https://www.droidcon-boston.com/speakers/ and look for "Anton Spaans"
But for short, like @gildor said, you can use the Rx and Reactive adapters from JB.