https://kotlinlang.org logo
#coroutines
Title
# coroutines
f

Fredrik Larsen

04/05/2019, 7:30 AM
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

gildor

04/05/2019, 7:41 AM
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

tseisel

04/05/2019, 7:43 AM
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

Fredrik Larsen

04/05/2019, 8:19 AM
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

gildor

04/05/2019, 8:20 AM
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

streetsofboston

04/05/2019, 11:15 AM
I'll be giving a talk about exactly this question, @Fredrik Larsen, at DroidCon Boston next Tuesday! :)
f

Fredrik Larsen

04/05/2019, 11:23 AM
Thank you for the input! @gildor @streetsofboston cool! I hope it'll be available online? 😉
s

streetsofboston

04/05/2019, 11:24 AM
It will be available after the conference.
f

Fredrik Larsen

04/05/2019, 11:25 AM
That's great 🙂 Good luck. I'm looking forward to seeing it.
s

streetsofboston

04/05/2019, 11:26 AM
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.
3 Views