How to know which methods in `<http://java.io|java...
# coroutines
m
How to know which methods in
<http://java.io|java.io>
should be called using
<http://Dispatchers.IO|Dispatchers.IO>
dispatcher? For example
File.exists
File.canRead
etc
l
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
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.
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
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
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
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.