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

Pablo

02/16/2021, 4:02 PM
Hello if my presenter is written in java and my datasource is in kotlin and I have used coroutines, how do I connect them? Do I have to use the
suspendCancelableCoroutine{..}
? Then do i need the
launch{}
somewhere? Or from the java class I call the method of my presenter and that's it? I can avoid the launch?
d

diesieben07

02/16/2021, 4:16 PM
Java cannot interact with suspending functions in a meaningful way. You have to expose any suspending functions that you want to use from Java in a way that Java can consume - i.e. as a
CompletableFuture
or something similar. E.g.:
Copy code
suspend fun doSomething(): String = TODO()
fun doSomethingFutureAsync(): CompletableFuture<String> = future { doSomething() }
This uses
future
from `kotlinx-coroutines-jdk8`: https://github.com/Kotlin/kotlinx.coroutines/tree/master/integration/kotlinx-coroutines-jdk8.
p

Pablo

02/16/2021, 4:18 PM
so no matter what I need to do a suspend fun?
I mean if my presenter is in Java and I have a datasource that has de suspendCancellableCOroutine this method has to be suspend right? otherwise it won't work
d

diesieben07

02/16/2021, 4:21 PM
Well, you can also just do
Copy code
future {
    // suspending code here
}
Which produces a
CompletableFuture
directly - and lets you suspend.
6 Views