SEQUENTIAL Background Tasks with Kotlin Coroutines (async and await)

Поділитися
Вставка
  • Опубліковано 6 вер 2024
  • SEQUENTIAL Background Tasks with Kotlin Coroutines (async and await)
    High quality android courses: codingwithmitc...
    In this video I take you through an example of how to execute work on a background thread sequentially with kotlin coroutines and the async and await pattern.
    Async and await is great when you need access to a background task result within the coroutine. Using the more classic job launcher pattern requires much more code. Async and await is definitely the better choice if you need access to a result inside the parent coroutine.
    Source code: bit.ly/2MqDLSf
    Instagram: / codingwithmitch
    Twitter: / mitch_tabian
    .
    .
    .

КОМЕНТАРІ • 62

  • @victornorthhead1384
    @victornorthhead1384 4 роки тому +3

    Mate, I’ve learned a lot more from you than from the official Google documentation.

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      Consider leaving a testimonial friend 😬. codingwithmitch.com/testimonials/

  • @kristianczepluch6147
    @kristianczepluch6147 4 роки тому +5

    Wait, cant you just use suspend functions which are sequential by default?

  • @whoadityanawandar
    @whoadityanawandar 3 роки тому +2

    Thank you for the video. But I didn't get one thing, why is async await being used here, aren't coroutines executed sequentially by default??!

    • @leonardosibela
      @leonardosibela 3 роки тому

      Yes they are and I think this is not a good example. But I'd like to hear it from Mitch in case I'm not getting something.

  • @kahanbhalani3607
    @kahanbhalani3607 2 роки тому +2

    Wouldn't we get the same result if just called getResult*FromApi() calls as it is:
    val result1 = getResult1FromApi()
    val result2 = getResult2FromApi(result1).
    Wouldn't this work as we are using suspending functions so that before getting result2 our code will be suspended?

  • @biblecartoonsforall
    @biblecartoonsforall 2 роки тому +3

    Don't suspend functions execute sequentially by default? If so, why the need for async and await?

    • @kahanbhalani3607
      @kahanbhalani3607 2 роки тому

      I've the same question. Why should we use async/await?

    • @azekDv
      @azekDv 10 місяців тому

      ​@@kahanbhalani3607 async is a coroutine builder, so block of code inside async executed in his own coroutine. That means they executed in "parallel", not sequantially. In order to get a result of the coroutine execution we call await() which is a suspend function and executed sequentially. So, block inside asyncs executed in parallel, but the result of their execution retrieved sequentially.
      In this particular example, there is really no need for async if you call await() right after async.

  • @VikramYadav-vn2nq
    @VikramYadav-vn2nq 4 роки тому +2

    How is it different from normal launch like in your beginner coroutine example?

  • @baijusharma6027
    @baijusharma6027 4 роки тому +1

    If possible please make a video on Kodein. Also, we would like to know is it easier and more beneficial than Dagger2. You are simply a great teacher. Thankyou :)

  • @jabrane1564
    @jabrane1564 3 роки тому

    thanks for these videos.
    Im just starting with coroutines and im not gonna lie , this looks intimidating !

  • @rohithegde9239
    @rohithegde9239 4 роки тому +1

    Awesome tutorials! A suggestion: You can go a little deeper into these topic.

  • @zohaib9381
    @zohaib9381 4 роки тому +3

    Can you make a video on how to implement download images from a URL using a coroutine

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      Download and save to internal memory or just display the image in an image view?

    • @zohaib9381
      @zohaib9381 4 роки тому +1

      @@codingwithmitch saving to internal memory, I want to implement the download feature for a wallpaper app but can't find any good resources where I can learn how to implement downloading image to device storage, as I can apply it as a wallpaper only if it exists in the storage(by taking its Uri)

    • @petrushoc
      @petrushoc 4 роки тому

      github.com/hoc081098/ComicReaderApp_MVI_Coroutine_RxKotlin_Jetpack
      My app have download images functionality (using Work Manager + Coroutines), save to fileDirs, and show to recyclerview

  • @Lovepreet-xs5qi
    @Lovepreet-xs5qi Рік тому

    Thanks Mitch❤

  • @bhuvneshvarma7843
    @bhuvneshvarma7843 3 роки тому

    Good video.. why are you not using LifeCycle Scope in activity?

  • @JujareVinayak
    @JujareVinayak 4 роки тому

    What is difference between previous video and this video. How this video has sequential tasks?

  • @prasannadeshpande4435
    @prasannadeshpande4435 3 роки тому

    Thanks for the video.

  • @codewithpreeti4083
    @codewithpreeti4083 3 роки тому

    Thanks . I have a question ,what if I want to return Execution time from fun fakeApiRequest to some other fun say ..func abc . In this case how to return the value from here and how to retrieve in fun abc ?

  • @yousefzk22
    @yousefzk22 4 роки тому +1

    thanks a lot about Coroutines , can you make a video about set daynight theme in android (Dark/Light) on kotlin :) thank you

  • @shahzebmushtaq1297
    @shahzebmushtaq1297 4 роки тому +1

    great video . can you please make some real example that is actual implementation of executing tasks using coroutines or background threads

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      I have lots:
      1) codingwithmitch.com/courses/model-view-intent-mvi-architecture/
      2) codingwithmitch.com/courses/powerful-android-apps-with-jetpack-architecture/

  • @JC-oe8ex
    @JC-oe8ex 3 роки тому

    it crash when i after i clicked the button and wait for almost 10mins. im using emulator. can i get a feedback here?

  • @cslesin5750
    @cslesin5750 4 роки тому +1

    It’s so wired,why async/await become sequential?

  • @mcircle6489
    @mcircle6489 4 роки тому

    Why await not showing in my coroutine pls help

  • @adityagupta375
    @adityagupta375 2 роки тому

    Great content

  • @aliciabeltran
    @aliciabeltran 3 роки тому

    Thanks for the video

  • @azekDv
    @azekDv 10 місяців тому

    I think it is not a good example if you invoke await() immediately after async. There is no sense. You just can use suspend functions without async. It is just not a case for using async

  • @ruifernando8066
    @ruifernando8066 4 роки тому

    what if there is a scenario when we need to call 3 API sequentially (A,B,C). If there is an error occurs during the execution (ex. calling B), how to handle it in clean way? so all the API call will be canceled

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      There's lots of different ways to do that. I show you my personal favorite way in the "powerful Android apps with jetpack architecture" course.
      Totally streamlined and seamless error handling. Also cancellations by the user when the back button is pressed or they leave the view.

  • @srjlove2102
    @srjlove2102 4 роки тому +1

    Thanks for more video...

  • @jayceeddeyro1406
    @jayceeddeyro1406 4 роки тому

    It will be different scenario if using viewmodel and livedata?

    • @berkc5323
      @berkc5323 3 роки тому

      Yes , you can just observe one livedata object and when it has the required answer you just call the other function with that answer. So you don't need to use async + await.

  • @aliakkawi4759
    @aliakkawi4759 4 роки тому

    Great video, thanks a lot.

  • @techmarinar
    @techmarinar 3 роки тому

    Thank you very much 💟💟

  • @jitendrapratap9667
    @jitendrapratap9667 4 роки тому

    he mitch, i saw you using your own physical device with android studio in one of your videos,how can i do that
    with my own physical device.

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      watch this ua-cam.com/video/UoBgEn04rLw/v-deo.html

  • @iwon6644
    @iwon6644 4 роки тому

    Great work btw. but how async/await become sequential here..

  • @ajaykumarvishwakarma4720
    @ajaykumarvishwakarma4720 3 роки тому

    Cool👍

  • @mb1-9999
    @mb1-9999 4 роки тому

    Can you make a video about jackpack compose

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      It's not mature enough yet imo. I will in some time

  • @chibuikeoffu
    @chibuikeoffu 4 роки тому

    Hi Mitch, nice video as always please how do I use coroutines to wait for data from a listener before continuing execution.
    var token = ""
    val mUser= firebaseAuth.currentUser
    val userToken = mUser?.getIdToken(true) userToken?.addOnSuccessListener {
    token = it.token!! //the value here is actually set }
    return token }
    I want to wait for the listener is set before i proceed

  • @ismailissejama9773
    @ismailissejama9773 4 роки тому

    hi mitch what is next project after (
    Powerful Android Apps with Jetpack Architecture)
    ------- would teach us how to use websocket.io for real time database in android using rest api?....

  • @jaserq
    @jaserq 4 роки тому

    Can we do everything what we do in rxjava with coroutines ?

    • @blueheartorangeheart3768
      @blueheartorangeheart3768 4 роки тому

      jaserq what do you with rxjava?

    • @jaserq
      @jaserq 4 роки тому

      1- please add tutorials repository wwith coroutines mvvm
      2 -data classes in kotlin doesnt have inheritance. Can you add video about that strategy .

  • @KIRILLINGUSS21
    @KIRILLINGUSS21 4 роки тому

    ure awesome/ thnak you

  • @arpitgupta3151
    @arpitgupta3151 4 роки тому

    Wait I thought your name is Mitch 😂
    BTW can you create videos on how to use any type of APIs using retrofit for apps?

    • @codingwithmitch
      @codingwithmitch  4 роки тому +1

      Ya here codingwithmitch.com/courses/powerful-android-apps-with-jetpack-architecture/