Android Studio / Idea gives me nice hints in my co...
# coroutines
Android Studio / Idea gives me nice hints in my code. Sometimes, A function call is marked with a yellow background because it's an «inappropriate blocking call». But why does such a blocking call become inappropriate when I add the suspend marker to my function? Especially in android code, I use coroutines to escape the main thread. So when I launch, I'm off of the main thread because I know my code might block. Can anyone point me to some reading that explains why the call is more inappropriate in a suspend fun?
blocking in coroutines is still bad, because you should either (a) suspend, or (b) offload to the IO dispatcher/threads. the reason for this is that the default dispatcher only has
# of logical cores
threads to work with, so if you're on a single core system, you'd block your entire application from functioning! the IO dispatcher is where those calls must be, because it has 64 threads by default, making it much harder (but not impossible) to block your application by filling these up, because if all of the IO threads are full the default dispatcher can still function
actually, the default dispatcher has a minimum of 2 threads, but if you block both of them, this still applies
But the essence of what you are saying is that Idea and AS marks this as inappropriate because they cannot know for certain on which dispatcher I am running the suspend function?
yes, I think there is improvements to this in the works, but in general if you put it on the right dispatcher close enough to the code where intellij can know for sure, it won't warn about this. the easiest way to get rid of it is to do
withContext(<http://Dispatchers.IO|Dispatchers.IO>) { blockingCall() }
, if you're already on the IO dispatcher this won't cause a re-dispatch and should be pretty fast
Thanks, then I see why the IDEs act like they do and no longer worry about my own understanding of it being completely wrong.