Тёмный

Suspend Functions - Kotlin Coroutines 

Philipp Lackner
Подписаться 183 тыс.
Просмотров 72 тыс.
50% 1

Опубликовано:

 

1 окт 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 85   
@marinbeslo7841
@marinbeslo7841 3 года назад
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.
@simpleuxapps8759
@simpleuxapps8759 2 года назад
Thank you for clarifying that
@Joaquin5992
@Joaquin5992 2 года назад
Thanks. I got confused in this.
@kayveekhatrao2486
@kayveekhatrao2486 Год назад
thank you for the clarification
@jhNic
@jhNic Год назад
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")
@ninelivesforge6432
@ninelivesforge6432 Год назад
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)
@bjugdbjk
@bjugdbjk 3 года назад
One key takeaway, code inside the launch is synchronous, Execution will wait until each line gets completed.
@nexgen.graphics
@nexgen.graphics 4 года назад
Finally I am starting to understand coroutines. You are doing a great job Sir.
@PhilippLackner
@PhilippLackner 4 года назад
Glad to hear that man, congrats! :)
@priyanktyagi89
@priyanktyagi89 9 месяцев назад
@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 ❤️😊🙏
@cosovic14
@cosovic14 4 года назад
Since you can actually run regular functions inside of a corountine, what is the actual purpose of a suspend function? What's the point?
@PhilippLackner
@PhilippLackner 4 года назад
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
@cosovic14
@cosovic14 4 года назад
@@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?
@PhilippLackner
@PhilippLackner 4 года назад
@@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
@jordan2816 4 года назад
@@cosovic14 you have to make test function "suspend"
@cosovic14
@cosovic14 4 года назад
@@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.
@tejas5331
@tejas5331 3 года назад
I have been watching your videos from a week now and I have to say I have never understood things better than ever.
@PhilippLackner
@PhilippLackner 3 года назад
Thank you❤️
@hannanshaikh2616
@hannanshaikh2616 4 года назад
delay and suspend functions are basically same, so how can we identify usage scenario means when to use delay and where suspend functions
@rabiunislam9302
@rabiunislam9302 4 года назад
Very easy and interactive way to teach coroutines. Keep it up.
@AlgeriaHere
@AlgeriaHere 4 года назад
Helpful video thank you
@PhilippLackner
@PhilippLackner 4 года назад
You're welcome
@khemmahato8420
@khemmahato8420 Год назад
does suspend function launches new coroutine or not please answer??
@play.againn
@play.againn 4 года назад
Best channel ever !
@PhilippLackner
@PhilippLackner 4 года назад
Thank you so much ❤️
@yutaitadori7318
@yutaitadori7318 3 года назад
Absolutely 😍😍😍😍😍
@ZaidZakir
@ZaidZakir 4 года назад
these videos are freaking excellent. i will recommend to all my android buddies. +1000000000
@PhilippLackner
@PhilippLackner 4 года назад
Thanks ❤️
@pavelb3734
@pavelb3734 4 года назад
Very nice. Hope you will tell us about the influence of the delay on the delay in next videos)
@androidpc9656
@androidpc9656 Год назад
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
@iamansinghrajpoot
@iamansinghrajpoot Год назад
I was wondering the same. He did not mention in the video.
@berkaytuzel1069
@berkaytuzel1069 3 года назад
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?
@suntvhits
@suntvhits Год назад
Good explanation, But you didnot mention How can we run both functions in different time!!
@haykmkrtchyan7093
@haykmkrtchyan7093 3 года назад
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?
@naskar23
@naskar23 4 года назад
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" }
@PhilippLackner
@PhilippLackner 4 года назад
Yes because you put the network calls in the log statement. In my example we first got both the results and then printed both together
@mikec4220
@mikec4220 4 года назад
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.
@amirsalarsalehi4968
@amirsalarsalehi4968 3 года назад
cause you are logging message after call, it takes 6 second. if you logging result after each call, it would be 3 second.
@VenewebTV
@VenewebTV 3 года назад
Excellent Video, very useful, regards from Caracas, Venezuela :)
@brianmason3331
@brianmason3331 3 года назад
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.
@ninelivesforge6432
@ninelivesforge6432 Год назад
yeah, i came from part 7 of jetpack compose to this playlist, roadmap would be nice.
@swapnilkadam1391
@swapnilkadam1391 2 года назад
commenting for YT algo
@shamsularefin6491
@shamsularefin6491 3 года назад
Amazing!!! just keep doint this type of amazing videos. Thanks for the videos.
@silvestreramirez4515
@silvestreramirez4515 2 года назад
Should I switch Dispatcher y every network call or set dispatcher in the coroutine?
@mehedihasansarik8427
@mehedihasansarik8427 3 года назад
I would subscribe to your channel 10000000 times if there were any such option.
@abondutta
@abondutta 3 года назад
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
@haissayf
@haissayf 4 года назад
you got a new subscriber
@PhilippLackner
@PhilippLackner 4 года назад
Welcome to the cool kids!
@Sk-gb2hx
@Sk-gb2hx 3 года назад
+
@crazyprogrammer3249
@crazyprogrammer3249 4 года назад
Bro what's the difference when we call suspend or non-suspend function in coroutine?
@crazyprogrammer3249
@crazyprogrammer3249 4 года назад
@@devkazonovic thank you so much bro❤️ finally I understood
@adnanjr
@adnanjr 3 года назад
But you didn't explain what is a suspend function and why to use it.
@moviesclips8539
@moviesclips8539 3 года назад
great work... very helpful and organised
@dawoodamir2011
@dawoodamir2011 4 года назад
Thanks for this awsome content man.
@PhilippLackner
@PhilippLackner 4 года назад
You're welcome man!
@MDDanishAnsariDev
@MDDanishAnsariDev 4 года назад
Nice 👌🏻
@PhilippLackner
@PhilippLackner 4 года назад
Thanks!
@Shasha-zs4dx
@Shasha-zs4dx 2 года назад
Clear and Precise with explanation..
@Kunal-jp8tn
@Kunal-jp8tn 2 года назад
Nice explanation. Thanks.
@RahulPawar-ok3nc
@RahulPawar-ok3nc 4 года назад
You deserve a lot more subscribers
@PhilippLackner
@PhilippLackner 4 года назад
Thank you!!!
@mrpi230
@mrpi230 4 года назад
Thank You good explanation.
@karthikss6501
@karthikss6501 3 года назад
Very clear explanation. Nice work. Thanks dude
@PhilippLackner
@PhilippLackner 3 года назад
You're welcome!
@SirDanMartin
@SirDanMartin 3 года назад
Nice clear and informative video, cheers.
@koutheirelbehi4384
@koutheirelbehi4384 3 года назад
philipppppppp is born to be a teacher !!
@VCodes
@VCodes 4 года назад
thank you so much. I will make sure to leave a like on every video i see. thats the least I could do.
@PhilippLackner
@PhilippLackner 4 года назад
Thank you! 🙏
@yashs1373
@yashs1373 Год назад
Great explanation
@ajaygalagali5963
@ajaygalagali5963 4 года назад
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?
@PhilippLackner
@PhilippLackner 4 года назад
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
@ajaygalagali5963
@ajaygalagali5963 4 года назад
@@PhilippLackner Got it. Thanks
@daiyrkanybekov8575
@daiyrkanybekov8575 3 года назад
Good tutorial Bro!
@roopaligoyal7916
@roopaligoyal7916 4 года назад
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.
@OhhhThatVarun
@OhhhThatVarun 4 года назад
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.
@roopaligoyal7916
@roopaligoyal7916 4 года назад
@@OhhhThatVarun thankyou !!
@jawadkakar1950
@jawadkakar1950 4 года назад
@@OhhhThatVarun I thought doNetworkCall() and doNetworkCall2() are executed in parallel. So both might take say 4 second or so.
@harishv4806
@harishv4806 4 года назад
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.
@NeitonSilva
@NeitonSilva 4 года назад
Simply the best!
@JuanDanielOrnella
@JuanDanielOrnella 4 года назад
You are excelent!!
@PhilippLackner
@PhilippLackner 4 года назад
Thank you! 😃
@hassanammer7854
@hassanammer7854 4 года назад
Great work😍 If you can speak slowly to let us write notes behind you ❤️
@SirDanMartin
@SirDanMartin 3 года назад
You can pause the video for that. I like his speed because I didn't want to watch a 15 minute video to cover a simple topic like this.
Далее
Coroutine Contexts - Kotlin Coroutines
5:39
Просмотров 63 тыс.
Being Competent With Coding Is More Fun
11:13
Просмотров 84 тыс.
小路飞嫁祸姐姐搞破坏 #路飞#海贼王
00:45
Coroutines: Concurrency in Kotlin
30:22
Просмотров 11 тыс.
5 Fatal Coroutine Mistakes Nobody Tells You About
18:15
Jobs, Waiting, Cancelation - Kotlin Coroutines
9:32
Просмотров 60 тыс.
All Rust features explained
21:30
Просмотров 310 тыс.
Premature Optimization
12:39
Просмотров 812 тыс.
Don't Use Polly in .NET Directly. Use this instead!
14:58
Suspend functions - Kotlin Vocabulary
9:03
Просмотров 50 тыс.