what is the best dispatcher to use for user input?
# coroutines
f
what is the best dispatcher to use for user input?
z
Doesn't matter
👍 1
Likely the main dispatcher though
c
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
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
what dispatcher should I use if I'm both reading a file and parsing it?
z
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
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
It's not Android 😄
z
Oh gotcha. What platform?
f
Jvm desktop
z
Must be nice not to have to deal with config changes lol
g
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