Just to mention, the networkCall() methods return the result after 3 seconds to their respective variables, but it needs 3+3=6 seconds to write them in the logcat.
Am I understand it right? val num1 = doNetworkCall() //delay 3s then return val num2 = doNetworkCall2() //delay 3s then return Log.i("TAG","From $num1") //will start running this line at 6s Log.i("TAG","From $num2")
changing the order helped me understand it better =) val networkCallAnswer = doNetworkCall() Log.d(TAG, networkCallAnswer) //appears in log after 3 seconds val networkCallAnswer2 = doNetworkCall2() Log.d(TAG, networkCallAnswer2) // appears in log after 3 more seconds (6 sec total)
@PhilippLackner , your videos are really very helpful in learning things quicker. However, I found 1 mistake in this video. You mentioned at 5:30 in your video. If main thread close than worker courotine's globe won't print. But here, you are using Global Scope. So, it would print. Perfect example would have been using to prove my statement would be using startActivity(Intent(this, SecondActivity::class.java)) finish() after printing main thread log. Thanks for helping us, keep posting ❤️😊🙏
As the name says a suspend function can be suspended. It can execute other suspend functions that otherwise could only be called from within a coroutine. For example delay is a suspend function and you can't call that outside of a coroutine but inside of a suspend function
@@PhilippLackner but if I have for example fun test{ Println("hello") } I can call this inside of a coroutine like so : launch { test() } So, what would be the point in putting suspend in front of my function name?
@@cosovic14 nothing would be the point, because your test function doesn't call other suspend functions. But if you would want to delay your hello print for 1s then you would have to put it into a suspend function or directly in a coroutine because you can't call delay from a normal function. Network calls are also suspend functions for example, so when you want to make a network call from a normal function, you need to make it a suspend function (if you use coroutines for that network call)
@@jordan2816 Thanks. I'm curious as to what happens when the function gets suspended, I know it suspends the coroutine to allow the thread to go do other work, but I read that it does its suspended task on another thread then can resume in another thread in the pool. It's a bit confusing to me.
is the Suspend function used only to restrict a function to call inside a Coroutine scpoe or suspend function ? is there any other reason to use suspend function
Hey got a quick question, if instead of assigning values to doNetworkCall and doNetworkCall2, we just said Log.d(TAG,doNetworkCall()) would it make a difference?
Oh so that's the thing, when people say that suspend functions can be paused and resumed, actually they pause and resume the coroutine, in which scope they are running right?
Awesome explanation. I have a doubt regarding the adding up of delay times. I tried following the same example, but in my case, it took 3 secs to print the first function response and after another 3 seconds, it printed the second function response unlike how it printed both the statements after 6 seconds. Is there a reason why it is happening like that? GlobalScope.launch { Log.d(TAG,doNetworkCall()) Log.d(TAG,doNetworkCall2()) } suspend fun doNetworkCall():String { delay(3000L) return "this is a custom suspend function" } suspend fun doNetworkCall2():String { delay(3000L) return "this is a custom suspend function2" }
To have them one after another and not at the same time you just need to print after you get the first result and then print again after the second. val networkCall1 = doNetworkCall1() Log.i("network", "$networkCall1") val networkCall2 = doNetworkCal2() Log.i("network", "$networkCall2") Hope this helps.
Is there any roadmap we should take when watching your videos? I started with the Kotlin course then moved on to the android fundamentals course until I realized that I needed to learn some Jetpack Compose and started that until learning I needed to learn about coroutines and am now here.
if i run a corutine for a long time, then the screen stays blank for long time. if i can show the screen as circular loading then it will be great. Ex: when doing a http request, if i can show a loader dialog then the user will know process is running
3:47 It should print first return statement and take 3 sec break and print second return statement, right? But here it takes 6 sec break together, why?
No, both suspend functions are executed in the same coroutine and we only log after both functions are done, so after two delays it will print both logs
Why is the delay added up? why is it not, that the first "This is the answer" statement printed after 3 second and the next after 3 more seconds? Could you please help to clear my doubt.
Delay was added up because it took 3 sec to execute doNetworkCall() and then again it took 3 sec to execute doNetworkCall2() so doNetworkCall2() was called after 3 sec (because doNetworkCall() took 3 sec) and then again the Log statement was called after 3 sec (because doNetworkCall2() took 3 sec) so the overall time taken to call the Log statement is 3+3=6 sec.
Just change the log. GlobalScope.launch { val networkCallAnswer = doNetworkCall() Log.d(TAG, networkCallAnswer) val networkCallAnswer2 = doNetworkCall2() Log.d(TAG, networkCallAnswer2) } This would be returning the first network call within 3 sec.