Async and Await - Kotlin Coroutines

Поделиться
HTML-код
  • Опубликовано: 25 ноя 2024

Комментарии • 121

  • @iam-learning6374
    @iam-learning6374 4 года назад +86

    Just Conclusion of above video:: [For beginners only]
    there are basically two kind of execution
    1. Sequential (Default)
    2. Concurrent(achieved with async{} )
    For instance if we have two function func1() & func2(), execution of func2() is dependent on func1() then in that case we can use default version of coroutine and if they are independent of each other then we can use concurrent with the help of async{}
    with the sequential execution we run the code sequentially so if func1() takes 5sec for execution and func2() takes 3 sec for execution then total elapsed time will be around 8 sec, however when this methods are independent and their results are not required for execution of other fun then in that case execution can be completed in 5 sec with the help of async(concurrent).
    Async vs launch:
    Async returns Deffered [Type:: calling function is returning]
    launch returns Job.
    await() -> methods are used to ensure the execution will go further until function is executed completely. [e.g func1().await()]
    We can also call async with LAZY pass below value in async constructor.
    val one = async(start = CoroutineStart.LAZY) { func1()) }
    one.start() // start the execution of func1
    Reference link:: (Highly recommended: please watch this video before exploring below link)
    kotlinlang.org/docs/reference/coroutines/composing-suspending-functions.html

    • @Ilamarea
      @Ilamarea 2 года назад +10

      You are repeating a very incorrect statement that gets thrown around a lot with coroutines.
      All coroutines are sequential.
      What produces concurrent code are nested coroutines.
      Async does not produce concurrent code without nesting. It just returns the value.
      You decide how the code is blocked depending on where you put the .await() or .join() functions.

    • @sameeryadav8857
      @sameeryadav8857 2 года назад

      @@Ilamarea correct

  • @kahanbhalani3607
    @kahanbhalani3607 2 года назад +20

    This is the easiest explanation I've found on Kotlin Coroutines. Very, well done. 👏👏

  • @BurzowySzczurek
    @BurzowySzczurek 10 месяцев назад +1

    So I just watched the entire series (except android specific things because I don't make android apps) and I really helped a lot. I finally understood those thingies.
    I knew the async mechanics from other languages like Dart and a bit of JS but I had hard time mapping the Kotlin syntax to what was used to.
    This did really help.

  • @aviksutar
    @aviksutar 3 года назад +13

    whenever I starting watching your videos.I just press the like button first. :). Thank you for such simplicity

  • @Ilamarea
    @Ilamarea 2 года назад +6

    I've read a whole bunch of blogs and posts and watched a ton of videos.
    This is the first thing that actually explains the difference between launch and async.
    Nice.

  • @warwickwestonwrigful
    @warwickwestonwrigful 2 года назад +14

    This video should have been extened to show how you would have updated a UI component with the data returned by the async function (in a thread safe manner)

  • @aryanvikash1967
    @aryanvikash1967 4 года назад +8

    Omg where was you .. seriously u made it so simple for me.♥️

  • @aritrabhattacharya7722
    @aritrabhattacharya7722 4 года назад

    your coroutine series is the best Period

  • @jayceeddeyro1406
    @jayceeddeyro1406 4 года назад +5

    I love all your videos!!! I am still shock why still not enough views. This is quality content to be honest.

    • @PhilippLackner
      @PhilippLackner  4 года назад +10

      Thank you!! ❤️ I think that's completely normal that RUclips needs its time to check who is actually serious with his videos and who not. In the long term my channel will grow

    • @jayceeddeyro1406
      @jayceeddeyro1406 4 года назад

      @@PhilippLackner I really hope it will. keep it up! Will wait for your more videos!

    • @PhilippLackner
      @PhilippLackner  4 года назад +2

      @@jayceeddeyro1406 thanks man, I will just keep going thanks to comments like yours🙏

    • @PhilippLackner
      @PhilippLackner  4 года назад +2

      Hey man, would it be okay for you, if I use your comment as one of many testimonials on my future website?

    • @jayceeddeyro1406
      @jayceeddeyro1406 4 года назад

      @@PhilippLackner Yes of course! I am okay with it. Its my pleasure! :)

  • @norulesprateek
    @norulesprateek 4 года назад +8

    Neat explanation, understood the concept right away. Thanks!

  • @mahmoudabdelwahab1015
    @mahmoudabdelwahab1015 4 года назад

    علينا النعمه اسيادنا راضيين عليك Good Job dude really thank you very much

  • @rajkumarsingh8664
    @rajkumarsingh8664 3 года назад

    Fantastic video. Keep smiling and keep rocking.

  • @hossamqandel5638
    @hossamqandel5638 2 года назад

    Thanks Philipp for this ease and fully clear content ❤❤❤❤❤❤

  • @dawoodamir2011
    @dawoodamir2011 4 года назад +2

    Very well explained man

  • @sarthakgiri4596
    @sarthakgiri4596 8 месяцев назад

    great content Philip, you are awesome

  • @SouthernPole
    @SouthernPole Год назад

    you made coroutines so easy

  • @unaisulhadi9102
    @unaisulhadi9102 4 года назад +3

    Thanks for such a simplified, great tutorial :D Keep going, Also please make a tut about Dependency injection , Thanks

  • @smreha
    @smreha 2 года назад +1

    Nicely explained. Thanks.

  • @renatoosousa
    @renatoosousa 2 года назад

    The best explanation. TY

  • @ManishPatel3030
    @ManishPatel3030 4 года назад +2

    Very cool session. Thanks for this 👍

  • @Hrit
    @Hrit 2 года назад

    super clean explanation

  • @AlgeriaHere
    @AlgeriaHere 4 года назад +1

    great video so far , keep up the good work

    • @PhilippLackner
      @PhilippLackner  4 года назад +1

      Thanks man🙏

    • @AlgeriaHere
      @AlgeriaHere 4 года назад

      i already knew little bit about coroutines before but after watching your videos , they are not that long but showed me valuable information and i will keep watching your videos , thank you

  • @dbtechprojects2392
    @dbtechprojects2392 3 года назад +2

    Great tutorial which was helpful to me as a beginner, will be implementing this later :) thanks

  • @austin17ng
    @austin17ng 3 года назад

    thank u for your wonderful tutorial

  • @achyut_ft
    @achyut_ft 11 месяцев назад

    Great Explanation

  • @nanonkay5669
    @nanonkay5669 2 года назад

    I'm coming from web and my understanding is Deferred = Promise

  • @Raazvenga
    @Raazvenga 4 года назад

    Good Job. Explained it very well.

  • @elpirato5428
    @elpirato5428 3 года назад

    thx for the best simple exapmles

  • @Kunal-jp8tn
    @Kunal-jp8tn 3 года назад

    Thanks for the tutorial!!!!!!!!!!

  • @siddharthdixit5009
    @siddharthdixit5009 Год назад

    Your all videos are just awesome .please create a udemy course on Android advanced topics please

  • @moekyawoo9378
    @moekyawoo9378 2 года назад

    Nice explain..

  • @jonhdow4396
    @jonhdow4396 3 года назад

    very well explained.

  • @NeitonSilva
    @NeitonSilva 4 года назад +1

    Thanks for the videos!

  • @hi-rt4hc
    @hi-rt4hc 2 года назад

    thank you. understood fast

  • @jordan2816
    @jordan2816 4 года назад +5

    it is better to explain more if you try
    val answer1 = async{networkCall1()}.await()
    val answer2 = async{networkCall2()}.await()
    the result should be different.

    • @VivekSharma-qw5ky
      @VivekSharma-qw5ky 4 года назад +3

      then they would give answer sequentially rather than parallelly

    • @sanD-xq8nb
      @sanD-xq8nb Год назад

      @@VivekSharma-qw5ky Why it is so? Only the Deferred object run at the same time?

  • @abuiman5251
    @abuiman5251 2 года назад

    Thanks bro!

  • @mrpi230
    @mrpi230 4 года назад

    Thank You

  • @ahmedabolftouh3124
    @ahmedabolftouh3124 3 года назад

    Very nice 👍👍, you are simplyfying it very good

  • @bboydarknesz
    @bboydarknesz 4 года назад

    wow
    It reminds me Javascript again..
    async await..

  • @soumayamondal6915
    @soumayamondal6915 4 года назад

    Thank you sir for your help

  • @srikanthgutti5056
    @srikanthgutti5056 4 года назад

    Looking forward for your new udemy course

  • @triihart
    @triihart 2 года назад

    gud content. i like it

  • @robertradi7364
    @robertradi7364 4 года назад

    well explained!

  • @MrRahulmalik
    @MrRahulmalik 4 года назад +3

    Hey please make a video on how to consume GraphQl api with Apollo using MVVM and coroutines

  • @lial2011
    @lial2011 4 года назад

    great tutorial

  • @xmoore5377
    @xmoore5377 Год назад

    mantap bang messi

  • @davidgreenwitch
    @davidgreenwitch 2 года назад +1

    Does the await call block until it finishes? So do we wait longer if network call 1 takes say 10 seconds? And only then we see call1 and2 (which took only 3 seconds)?

  • @brucebane7401
    @brucebane7401 3 года назад

    amazing!!!!!

  • @MohsenMashkour
    @MohsenMashkour 2 года назад

    thank you, Philipp but i did not get why we should use await?

  • @harshar6897
    @harshar6897 3 года назад

    👍

  • @joelmikha1792
    @joelmikha1792 Год назад

    Hi btw what imports you have in your project? Becuse i cannot use some methods/functions that you use for the video on my android kotlin
    project

  • @rak590
    @rak590 2 года назад

    why did you hve to use globalscope coroutine? would we be able to do this without the global scope coroutine?

  • @minaisaac4529
    @minaisaac4529 3 года назад

    brilliant..but is there any use case for one "async" alone ?

  • @ajaygalagali5963
    @ajaygalagali5963 4 года назад

    :) Thanks

  • @victory4627
    @victory4627 3 года назад

    Can you please make videos on Dagger because it's really hard to implement in real-life projects.

  • @danielcettour
    @danielcettour 2 года назад

    I'm in a fragment and after onCreate(), onCreateView() is called, the problem is that I need the data retrieved from the service in onCreate to use it in onCreateView, but what happens is that the code keeps going and crashes, the problem is that it has no data because is being obtained in the corrutine in onCreate, like your method. How can I wait for corrutines to retrieve data before continuing to onCreateView()? Hope I made myself clear, thank you!

  • @poojanprajapati5553
    @poojanprajapati5553 2 года назад

    Is Deferred in kotlin and Future in flutter are same concepts?

  • @mikemikee7797
    @mikemikee7797 4 года назад

    GJ as always is there any chance to make a video about web sockets?

    • @PhilippLackner
      @PhilippLackner  4 года назад

      Not planned currently, but someday I want to do something with client server communication

  • @vimaljames6440
    @vimaljames6440 2 года назад

    Why do we require to use GlobalScope.launch? Can we not write the coroutineScope {} ?

  • @srikanthgutti5056
    @srikanthgutti5056 4 года назад +2

    Here still i'm not clear why i should go for async when i can able to make both requests in 3 seconds using launch().

    • @mikec4220
      @mikec4220 4 года назад

      I guess because with async you have a much leaner code...

    • @nurshuvo3854
      @nurshuvo3854 3 года назад

      How will u do it?

  • @pavelb3734
    @pavelb3734 4 года назад

    one question, as you asked to ask- on your label picture of the channel at the video beginning (where a green android is)- the word "ANDROI" - is something like hand, "D" - is a body. But I am still confused what "EVS" is?
    PS. Videos are great, thank you)))

    • @PhilippLackner
      @PhilippLackner  4 года назад

      Uhm read my name again and then look at the logo again😂

  • @AlfranAli
    @AlfranAli 2 года назад

    I think the async will create 2 separate threads and wait for it, in that case what is the use of suspend? In first way coroutine scope will run in a single thread for both network calls? I'm new to kotlin , someone please correct me if I'm wrong? How can we track how many threads are getting created, some good resources to read will be appreciated.

  • @mayanknagwanshi
    @mayanknagwanshi Год назад

    this is very different from javascript async await, I was thinking the await() function will block the code execution until the async function completes, in fact this is opposite

  • @mohammedoucif217
    @mohammedoucif217 4 года назад

    hi sir in the case when you use two launch functions you said we need to wait for both jobs to finish so the we don't see null for answer1 and answer2 .why !?

  • @talhac2796
    @talhac2796 2 года назад

    Why we should use async instead of launch?

  • @abdoghania2273
    @abdoghania2273 4 года назад

    WorkManager Tutorial please

    • @PhilippLackner
      @PhilippLackner  4 года назад +1

      Next project will be a bigger app, maybe after that

  • @mingato5538
    @mingato5538 4 года назад

    Make video about dependency injection took thank you in advance

  • @guy1407
    @guy1407 4 года назад

    Hi,
    Thanks for your videos
    Why did you change from val to var and back to val
    Why cannot all your variables be var for the demos (unless you want to declare a constant, like the TAG during the program that does not change, and will not change) ?
    For example - time = 04:48, var answer1 , val job1 turns to val answer1 @ 05:12

    • @PhilippLackner
      @PhilippLackner  4 года назад +2

      Only ever make a variable a var if you need to change it, everything else is a bad practice and is also marked by the IDE as a warning

    • @guy1407
      @guy1407 4 года назад

      @@PhilippLackner many thanks (peter?)

  • @pavelb3734
    @pavelb3734 4 года назад

    Is it correct to use delay to simulate network calls instead of sleep? Because in fact network requests are blocking in common cases.

    • @PhilippLackner
      @PhilippLackner  4 года назад

      Network calls don't block the thread if you use them in coroutines

  • @ARIHANTJAIN27
    @ARIHANTJAIN27 3 года назад

    What is the meaning Deferred?

  • @narangkishore98
    @narangkishore98 4 года назад

    Tried using async and it said to include anko library and when did async was deprecated.

  • @ravinsinghjain6529
    @ravinsinghjain6529 2 года назад

    You said its bad practice but you did not mention why it is bad practice. Can you explain that ?

  • @abdelrahmanesam3684
    @abdelrahmanesam3684 4 года назад

    what if i didnt call await ?

  • @cinemacom5161
    @cinemacom5161 2 года назад

    CONTROL + D !!

  • @debashishmahato4591
    @debashishmahato4591 4 года назад

    Can we have 1 more video for async await??

  • @FckYouPlz
    @FckYouPlz 4 года назад

    At 4:35 (the bad way) it took 3039 ms but in 6:41 (the better way) it took 3091 ms. Why?

  • @arunenquiry
    @arunenquiry 3 года назад

    At 4:49 : why is this a bad practice?

  • @Sk-gb2hx
    @Sk-gb2hx 3 года назад

    +

  • @xmoore5377
    @xmoore5377 Год назад

    bibi aku cantik banget bang