Hello everyone.... Am I going crazy? In my Android...
# coroutines
a
Hello everyone.... Am I going crazy? In my Android ViewModel I have something like this:
Copy code
viewModelScope.launch(<http://Dispatchers.IO|Dispatchers.IO>) { someFunction() }
....
fun someFunction(){}
It's compiling!!! But whyyyy? Shouldn't someFunction() be suspendable??
z
You can call regular, non-suspending functions from suspending functions. You just can’t call suspending functions from regular ones.
a
Well .... is it something new? I'm sorry for the stupid question
a
The only limitation is that you must invoke suspended functions in a suspended block If you could only invoke suspend methods in a suspended block you would not be able to do even a simple
val i = 0
as it’s synchronous
I’d say that is a consistent behaviour from the introduction of
suspend
z
Nope, that’s always been the case, and it’s very common to call non-suspending functions from suspending ones.
❤️ 1
☝️ 1
a
Okay guys, thank you so much for your help!
z
I really like how this article explains it
👏 1
❤️ 1
a
I'd just freaking love this community! Thank you so much ❤️
I def gonna read the article
@Zach Klippenstein (he/him) [MOD] but will the non suspending function block the calling suspending one? 🤔
z
Yes. However, because you’re launching your coroutine on the IO dispatcher, it will run on a background thread and it won’t block your app’s main thread.
a
Got it, thank you :)
g
One more addition, even if you mark function with suspend modifier, it doesn't mean that it automatically non-blocking, it just means that it can call other suspend functions
e
This is an excellent question, actually! Why would someone assume that non suspending functions should be callable from a coroutine context?