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

Herko

02/22/2019, 5:45 PM
Updated_the_above_snippet_for_a_bit_more_context_on_my_library_mixing_issue.kt
g

gildor

02/22/2019, 5:46 PM
I see now what you mean. Problem of your case, that this Java API doesn't support asyncronous supplier, it have to be syncronous.
You have 2 options: 1. Refactor Java code to support asyncronous supplier (so java should subscribe itself on supplier result) 2. If it's not possible, the only what you can do is use runBlocking, to block thread to get value syncronously
h

Herko

02/22/2019, 5:49 PM
g

gildor

02/22/2019, 5:50 PM
Also, I'm not sure that it will work for you real case, but you can first request for value from suspend function and when it will be ready subscribe on get and pass value that you already received asyncronously But it, of course, changes semantics of code, it's not so lazy as could be with asyncronous supplier support
What do you mean safe to use? Depends on your case, such call will block thread until your suspend function return value, safe it or not depends on your code
h

Herko

02/22/2019, 5:52 PM
The way the description is written just makes it sound like you shouldn’t be using it for production like activities.
I think option 1 might be more viable.
g

gildor

02/22/2019, 5:53 PM
Yes, usually you shouldn't use it, but it's a bridge that allows convert asyncronous coroutine to syncronous code
👍 1
h

Herko

02/22/2019, 5:54 PM
Thanks again for all the help.
g

gildor

02/22/2019, 5:56 PM
Yes, if you have access to Java code and can change it, I would just convert supplier to some CompletableFeature or other asynchronous primitive that can be used on java side and than you can easily use coroutines (see for example
future
coroutines builder, that crwates ConpletableFuture from suspend block)
2 Views