To stay in the spirit of coroutine, I prefer using await inside another coroutine scope to get the result instead of using onCompletion callback. Await() make handling exceptions easy because it re-throws any exception that was catched in async block and propagates it up into the hierarchy.
I had this exact problem a week ago, so I'm glad that now there's an easier way to find the solution because the problem took me a day or two to figure out, and I would have been so grateful for a resource like this.
This is the $1000 question! My guess is that you can't. I think you will need to assign the result to a flow variable that is observed by whatever needs it.
Well that's the most appropriate solution that I've found so far, for returning the value from a coroutine scope. If you have another approach, feel free to share it
@nidal hassan runBlocking should be used for tests mostly, but not in a production. With coroutines, you can't, and you shouldn't block the thread. Instead, you can use that approach like you have seen in the video.
I still have no idea why someone wants to use coroutines. If only ALL your unput-output is async and done using single global epoll system call - there is literally not even one reason to use coroutines.
@@dmitry.tupikin it is not easier (why does this video exist, if it is easier?), it is not lighter (at the end there is no other way of parallel execution, but threads). Coroutines work really better only on "sleep" example. Everything else just stuck in to plain old IO thread pool, that's all.
@@astroganov if you don`t understand smth it doesn't mean that it`s bad. Threads and async task is more complicated and heavier than coroutines to use. At one side, coroutines have more new material to dive into, but after all you get better flexibility and performance with more readable code.
@@danilkhailov3318 or maybe you just don't understand how it works under the hood, and use words like "heavier" just because someone (like you) said it, without really knowing what it should mean. Do you know how true async IO works? What epoll system call?
@@astroganov ok man, I got it. If you don’t wanna use coroutines just don’t, nobody pushes you. Moreover, you can use PC from 90’s because, for example, you can configure hdd work by yourself and use DOS for actually seeing how it all works under the hood.
You can use rurunBlocking function like that val result = runBlocking { viewModelScope.async { delay(3000) true }.await() } // Use the result Log.d(tag: "ExampleViewModel", msg: "$result")
You shouldn't use runBlocking this way. Here's what the docs say: "Runs a new coroutine and blocks the current thread until its completion. This function should not be used from a coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests."
What is the benefit of not launching a coroutine? Seems like an edge case, because the `.await()` from a `launch` block seems to provide the same functionality.
To stay in the spirit of coroutine, I prefer using await inside another coroutine scope to get the result instead of using onCompletion callback. Await() make handling exceptions easy because it re-throws any exception that was catched in async block and propagates it up into the hierarchy.
I had this exact problem a week ago, so I'm glad that now there's an easier way to find the solution because the problem took me a day or two to figure out, and I would have been so grateful for a resource like this.
This is one approach.
Another is to post values from the coroutine to a LiveData or a Flow that is most likely being observed on the UI.
Correct.
谢谢!
Thank you brother! 💙
WOW THANK YOU !, exactly what i was looking for !
Excellent video, I was looking for exactly how to do that, greetings from Caracas, Venezuela MUCHAS GRACIAS!!!!
Thank you very much for this. I'd like to make a request though, I really want you to do a series on testing, unit test and the likes. Please 🤲🏾
@Stevdza-San Thank you for the video. How can I return value in ViewModel function?
This is the $1000 question! My guess is that you can't. I think you will need to assign the result to a flow variable that is observed by whatever needs it.
Helped me at correct time thank you 😊
Hlo bro I want to talk to you i have a doubt 🧐 bro
Very good as always
How do you feel about returning values with a callback function, is there any particular drawback to it?
Well that's the most appropriate solution that I've found so far, for returning the value from a coroutine scope. If you have another approach, feel free to share it
Hlo jayrathod bro i want to talk to you i have a doubt 🧐
Thank you very much
Thanks, good👍👍👍
Thanks
Hello bro
If we need the value to complete the procedsure how we should await for it without continue executing the next lines of code 🧐
Use "await()"
@@StevdzaSan out side of the scope how? I used runBlocking to await the dataStore value for critical value but it seems not good for the main thread 😅
@nidal hassan runBlocking should be used for tests mostly, but not in a production. With coroutines, you can't, and you shouldn't block the thread. Instead, you can use that approach like you have seen in the video.
@@StevdzaSan if i need the value to return it over function not suspended what the best practise to do 😅
"Para você"
Am I the only who has listen this?
Waitttt did you make mvvm compose course in udemy man you sound sameee
At least some way to write an event based system with coroutine
Hi , 🤗
I didn't understand your video, speak quickly and undo the codes
I still have no idea why someone wants to use coroutines. If only ALL your unput-output is async and done using single global epoll system call - there is literally not even one reason to use coroutines.
Absolutely everything that you do using threads you can do with coroutines. It's easier, lighter and better in the context of performance.
@@dmitry.tupikin it is not easier (why does this video exist, if it is easier?), it is not lighter (at the end there is no other way of parallel execution, but threads). Coroutines work really better only on "sleep" example. Everything else just stuck in to plain old IO thread pool, that's all.
@@astroganov if you don`t understand smth it doesn't mean that it`s bad. Threads and async task is more complicated and heavier than coroutines to use. At one side, coroutines have more new material to dive into, but after all you get better flexibility and performance with more readable code.
@@danilkhailov3318 or maybe you just don't understand how it works under the hood, and use words like "heavier" just because someone (like you) said it, without really knowing what it should mean. Do you know how true async IO works? What epoll system call?
@@astroganov ok man, I got it. If you don’t wanna use coroutines just don’t, nobody pushes you. Moreover, you can use PC from 90’s because, for example, you can configure hdd work by yourself and use DOS for actually seeing how it all works under the hood.
You can use rurunBlocking function like that
val result = runBlocking {
viewModelScope.async {
delay(3000)
true
}.await()
}
// Use the result
Log.d(tag: "ExampleViewModel", msg: "$result")
You shouldn't use runBlocking this way.
Here's what the docs say: "Runs a new coroutine and blocks the current thread until its completion. This function should not be used from a coroutine. It is designed to bridge regular blocking code to libraries that are written in suspending style, to be used in main functions and in tests."
Why not just ‘.await()’ on the result? I’ve never seen ’invokeOnCompletion’ before…
Watch the whole video, I've talked about it. 😄
@@StevdzaSan Maybe im confused, and i see where you used `.await()` on the block, but why not use it on line 23, as in:
${result.await()}
?
@@ChrisAthanas Well, like I've said in the video, await is a suspend function, which means that we would have to call it inside a coroutine.
What is the benefit of not launching a coroutine? Seems like an edge case, because the `.await()` from a `launch` block seems to provide the same functionality.