https://kotlinlang.org logo
Title
f

Fudge

10/26/2019, 4:15 PM
what is the best dispatcher to use for user input?
z

zak.taccardi

10/26/2019, 4:21 PM
Doesn't matter
👍 1
Likely the main dispatcher though
c

Casey Brooks

10/26/2019, 4:22 PM
Typically, you’d want to handle UI events on the
Main
dispatcher, to ensure that the UI state is consistent with the data being processed. Depending on what you’re needing to do, you may need to receive the event on the
Main
dispatcher and then start a long-running task on a background dispatcher such as
IO
z

zak.taccardi

10/26/2019, 4:22 PM
Certain things you need to do on the main thread as required by the OS, but other than that it's unlikely to matter
f

Fudge

10/26/2019, 4:24 PM
what dispatcher should I use if I'm both reading a file and parsing it?
z

zak.taccardi

10/26/2019, 4:24 PM
I run everything in my UI on
Dispatchers.Main
and everything else on
Dispatchers.Default
. When using the default dispatcher, I use an ongoing Coroutine for thread safety. If there's blocking work, I use
Dispatchers. IO
for that work
<http://Dispatchers.IO|Dispatchers.IO>
for blocking IO work
👆 1
(reading and writing files)
f

Fudge

10/26/2019, 4:25 PM
But the parsing might take a lot of time too
basically what I have is the user gets an input popup, and while he types it parses some files
If you do any async work in an activity or fragment, remember it can be cancelled on a config change
Might be good to hoist that up into a view model, and use
<http://Dispatchers.IO|Dispatchers.IO>
to read/write to the files
f

Fudge

10/26/2019, 4:29 PM
It's not Android 😄
z

zak.taccardi

10/26/2019, 4:30 PM
Oh gotcha. What platform?
f

Fudge

10/26/2019, 4:31 PM
Jvm desktop
z

zak.taccardi

10/26/2019, 4:31 PM
Must be nice not to have to deal with config changes lol
g

gildor

10/26/2019, 4:52 PM
basically what I have is the user gets an input popup, and while he types it parses some files
Read input on any dispatcher, probably Main, than dispatch heavy work to another dispatcher (Read/Write file on IO, some CPU bound processing on Default)
This is the whole point of thread management with coroutines, you can easily switch between dispatchers
👍 1