A question regarding the inspection: Inappropiate thread-blocking method call
Note: I really value inspections to tell me what I am doing wrong, but I do not like something to be highlighted in yellow / red IF I cannot do something against it.
Why is walk highlighted…? I am doing the best I could. Or how could I change that? I thought wrapping a IO operation in Dispatchers.IO is best practise.
It is highlighted, because suspend functions are not supposed to perform blocking IO operations that might block them for a long time. File.walk does that.
On JVM you can perform some file related IO in a non-blocking manner with AsynchronousFileChannel but as far as I know it does not support file listing. Also, it is way too complex to worth the time in most cases.
If you actually need to call it from a suspend coroutine, you could:
create a channel
launch a tread with File.walk
in your suspend function wait for a value from the channel
at the end of that thread write into the channel
I don’t know if there is a simpler solution, but if so, I also would like to know.
Dispatchers.IO is perfectly fine for blocking calls. Oblviously you are wasting a thread per each blocking operation performed in parallel. But it’s irrelevant in your case, since you should not run many of FS walks in parallel anyway.
I believe in the past IntelliJ warned if we use blocking operations, but did not warn if doing this with Dispatchers.IO. Right now it seems to always warn. I’m not sure if this change was intentional or by accident.
Well, I had/have hard time understanding how am I supposed to use the scopes and dispatchers properly and I feel the documentation is in dire need of improvements on this particular topic.
For example, it says “do not use GlobalScope”. Ok, I understand why. So, what should I do… and here comes crazy documentation reading for 1 hour with no result.