https://kotlinlang.org logo
#android
Title
# android
k

KotlinLeaner

02/07/2023, 5:31 PM
Hi guys, Anyone can help me on
Flow
or
Channel
in kotlin? I want to build a
Queue
for commands. Thanks
p

Pablichjenkov

02/07/2023, 5:42 PM
I had this from some time ago. A simplistic version of an Actor. See if it can guide you a little bit. https://github.com/pablichjenkov/Android-Actor/blob/master/app/src/main/java/com/hamperapp/actor/Actor.kt Perhaps there is no need for the atomic check isStarted
In the case of flow, even though some operators are backed up by a channel and the backpresure is automatically handled by the coroutines suspend functionality . I still think the easiest approach to enqueue commands, is using a channel directly.
k

KotlinLeaner

02/07/2023, 5:55 PM
I tried with MutableSharedFlow in my project. Response time is very slow to respond back. I read that channel or flow will help to read data from BLE.
I'll try your sample. Thanks for great advice
p

Pablichjenkov

02/07/2023, 6:18 PM
Took a an overview of your code. The only thing comes to my mind, is that you are using emit in the shared flow without buffer, meaning the sharedflow will suspend until the consuming endpoint finishes processing the event. Perhaps giving it some buffer and changing the onbufferoverflow behavior speeds it up a little bit
Using a sharedflow with replay = 0, the defaults, also make sure your consumer gets connected before the flow emits, or the event will be lost
k

KotlinLeaner

02/07/2023, 7:03 PM
I don't have an idea of buffer in sharedflow
Replay = 0 is equall to buffer?
p

Pablichjenkov

02/07/2023, 7:45 PM
No, they are not equal. A sharedflow has both, replay and extrabuffer. When you set a replay different from 0, it will automatically set a buffer internally greater or equal than the replay but they are 2 different things. If you set extrabuffer and replay it will set the internal buffer to the greater of these 2 parameters. What you need is extrabuffer, let replay in 0 cause otherwise it will mess up you logic. Then pass also in the constructor the onbufferoverflow to drop last
This will allow to buffer emitted commands without suspending. In case your consuming function is slower than the one sending the commands
Emitting an item will take nanoseconds perhaps microseconds. The delay is usually the consuming side of the flow. Specially if UI is involved
k

KotlinLeaner

02/07/2023, 10:52 PM
Is it fine to set like this
Copy code
private val commandFlow =
    MutableSharedFlow<(input: String) -> Unit>(
        extraBufferCapacity = 1,
        onBufferOverflow = BufferOverflow.DROP_OLDEST
    )
?
Doesn't help anything if I used above code..
Same slow response coming from the BLE
p

Pablichjenkov

02/08/2023, 12:14 PM
Are you measuring the throughput of the channel/mutablesharedflow or are you measuring the ble messaging speed. If you refer to the second, it has nothing to do with coroutines at all. Is the phone hardware and the ble nature. I remember working on it and it was slow, by that time a ble characteristic read could only send 20 bytes per packet, it probably changed.
Ble is not meant to have a high throughput
k

KotlinLeaner

02/08/2023, 12:16 PM
Yes I am measuring the ble messaging speed.
p

Pablichjenkov

02/08/2023, 12:20 PM
Well, unfortunately there is not much you could do there. Perhaps, check if changing the mtu allows you for bigger messages. And that way you can receive more bytes, at least.
k

KotlinLeaner

02/08/2023, 12:20 PM
Okk I'll try the best. I really appreciate it...👍
👍 1
5 Views