https://kotlinlang.org logo
Title
b

bogdoll

07/19/2019, 9:47 AM
Hello all I got an external API which delivers data via an callback running on a different thread. How could I encapsulate those data into flows? An simple example, given an external API:
fun externalApi(query: String, callback: (Long,String)->Unit): Unit
I would like to convert this into something like
fun externalApiWrapper( query: String ): Flow<Pair<Long,String >>
Currently I have no idea how to do that.
b

bogdoll

07/19/2019, 10:49 AM
Thanks, that works for me. 🙂
g

gildor

07/19/2019, 11:39 AM
Just want to check, is this callback called multiple times, so is it stream of events?
b

bogdoll

07/19/2019, 11:57 AM
Yes, the callback will be called multiple times.
But I got one more question, regarding the back pressure. If it happens that the producer would be faster, how could modify specify the behavior of the "hidden" channel object? E.g. how could I block the thread of the producer, when I call the offer method. I did not really get the buffer method (mentioned in the comment of the link you provided).
g

gildor

07/19/2019, 12:12 PM
You can change behavior of hidden channel using buffer method, what is exactly not clear? You can just set channel capacity using it
If you want to block the producer's thread, you can wrap send() method with runBlocking
b

bogdoll

07/22/2019, 5:48 PM