Think about whether it's just reading into the memory (RAM), or whether it might need to read into the storage (for example to check whether the file exists or has read rights).
y
yschimke
01/09/2023, 9:51 PM
Any file operation, even ones you think should be almost free, can be delayed under certain load or system faults. So any file system ones.
yschimke
01/09/2023, 9:53 PM
And Dispatchers.IO is special, it shares threads with Default. So it's not a thread switch onto it usually. I believe it ends up being an accounting change effectively.
l
louiscad
01/09/2023, 10:02 PM
name, nameWithoutExtension, path, and the constructor of File/Path should be the only exceptions that don't actually touch the filesystem and can be looked into.
m
Mark
01/10/2023, 2:05 AM
Thank you all. Here is my situation. Suppose I have this:
Copy code
suspend fun someFun() {
someSuspendingFun() // suppose I know that this uses <http://Dispatchers.IO|Dispatchers.IO>
if (someFile.exists()) {
...
}
}
and I realise that
File.exists
might touch the file system. Should I solve this by:
Copy code
suspend fun someFun() = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
someSuspendingFun()
if (someFile.exists()) {
...
}
}
or:
Copy code
suspend fun someFun() {
someSuspendingFun()
withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
if (someFile.exists()) {
...
}
}
}
y
yschimke
01/10/2023, 3:13 AM
Probably doesn't matter. I'd probably do the second if you are already on default, so you don't accidentally have expensive CPU work on the IO dispatcher.