Kotlin Coroutines Beginner Example (Android)

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

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

  • @dhruvtatran5744
    @dhruvtatran5744 5 лет назад +119

    Coroutines, another strong reason to shift from Java to Kotlin
    Thanks for the video

    • @codingwithmitch
      @codingwithmitch  5 лет назад +3

      Yeah very strong. And this is just the surface of what it can do

    • @Sam-si7xv
      @Sam-si7xv 4 года назад

      Hi, i think im late here, but i need to ask about your statement. So, java cant use coroutines?

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

      @@Sam-si7xv nope

    • @Monkeydluffy-we2fk
      @Monkeydluffy-we2fk 3 года назад

      @nero I don't know y someone will use asynchronous task now instead of loaders or executors🙂

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

      @Saptarshi Samanta 1 year later, but yeah you can use kotlin with asynctask

  • @gilsonjuniorpro
    @gilsonjuniorpro 5 лет назад +58

    Finally, an easy way to understand coroutines, thank you, helped me a lot

    • @codingwithmitch
      @codingwithmitch  5 лет назад +1

      There's a lot more to it. This is a very very brief introduction

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

    A really good way to show an example and finally someone who actually knows the meaning of "beginners". Just recently started learning Kotlin and couldn't understand how coroutines work.
    This one is the best explanation possible for that, literally.
    Thanks a lot.

  • @ClintPaul89
    @ClintPaul89 5 лет назад +7

    Wow. This is like the simplest detailing someone can offer on Coroutines. I implemented it yesterday reading some other blog. I had my doubts still. Now it's all sorted. Thank you.

  • @sudhir1274
    @sudhir1274 4 года назад +6

    As a result of watching this video four times to fully understand the concepts of coroutines, I have successfully run a piece of code which for the past week kept crashing. This also works for normal Kotlin code outside of Android. Once again, thank you for sharing this video and explaining how coroutines work.

  • @kharagkunchok
    @kharagkunchok 4 года назад +7

    I always findmy self landing in your channel when ever I searched any issue. Thanks

  • @anirudhyeddula3411
    @anirudhyeddula3411 5 лет назад +4

    i've been reading several blogs and documentation, but this is just one video that made it look all so simple. thanks for the awesome video. Hope you keep churning out more of them.

  • @tanvimandanka
    @tanvimandanka 5 лет назад +10

    Just love the way he explains everything

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

    Thanks for making this video. The task of making sequential requests based on network responses drives me crazy! Thanks for this excellent video. I really appreciate it.

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

    This is very useful tutorial on running a "multi threaded" application in Android and Kotlin using Coroutines. Very well explained. This is better than Async and other thread related concepts. Thank you very much for sharing. You have explained in 23 odd minutes a concept I have previously not understood in spite of spending hours on tutorials and other videos. Much appreciated.

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

    It's been a while since I watched a tutorial that is not "copy this code and it will work". I fully understand something that I tought it was really messy. Thanks a lot man!

  • @khaledqwas8851
    @khaledqwas8851 5 лет назад +1

    Mitch, you are the best teacher of android on RUclips, keep it up.

  • @PaoloValerdi
    @PaoloValerdi 5 лет назад +14

    Great! Next timer I think it's better to run the coroutines in the lifeCycleScope or scope one to the current activity/fragment or even better the viewModelScope :)

  • @yasseraltamimi9060
    @yasseraltamimi9060 5 лет назад +5

    I watched many tutorials and it was so confusing. your tutorial made it very easy and now I think I can start using it.
    Thanks a lot.

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

    I read quite a lot about coroutines. This video is top notch at explaining it all. Well done and thank you!

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

    Nice explanation but small correction at 13:16 . Main, IO, Default these are the three co-routines dispatcher not scope.

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

    Better than the google tutorials, seeing the coroutines in action is way more simple to understand than abstract explanations. Thanks!

  • @rajbopche7992
    @rajbopche7992 5 лет назад

    At 0:29 to 0:32 you say RxJava is used for performing background tasks. I think this is a bit of an understatement. Thread switching is just one part of RxJava. There are hundreds of other operators present in RxJava that allows other functionality besides just Thread Switching. Just wanted to let you know so that thousands of others can benefit.
    Best part 7:00 to 7:45 The visual representation of thread and co-routine.
    I watch your content frequently and its absolute quality. Keep up the good work.

    • @codingwithmitch
      @codingwithmitch  5 лет назад

      Pretty sure I also said that RxJava has powerful operators. I meant when compared to coroutines. When comparing coroutines with RxJava you're strictly talking about threading.
      Although thats changing. Looks like coroutines also has operators now and will continue to get more.

    • @rajbopche7992
      @rajbopche7992 5 лет назад

      CodingWithMitch Yea, every now and then we have something new, thats the fun I think.

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

    This was all I needed to get started with Coroutines. Thank you!

  • @sharifurrahaman885
    @sharifurrahaman885 5 лет назад +1

    One of the best video to understand kotlin coroutines

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

    Superb Mitch, your way of explanation is too too... awesome in shot span of time with perfect example. Keep it up i watched all your lectures over android.

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

    Your way of explaining things is very unique, RESPECT.

  • @luisjaviercastroreyes9566
    @luisjaviercastroreyes9566 5 лет назад +1

    Thanks for the tutorial, the way you showed how coroutines work is intuitive.
    Love your English, it's so clear for non-native English speakers. Thumb up and new subscriber to your channel. Greetings from Mexico.

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

    Actually this is really impressive, you explained Coroutines in a direct and easy way which is probably more complex than understanding Coroutines itself! Great job man!

  • @gilong1311
    @gilong1311 3 года назад +1

    Hey Mitch, just tried it myself. Great tutorial, it cleared up my understanding of suspend functions. I was under the impression that delay() would suspend each suspend function and not the entire coroutine. Thanks for clearing that up!

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

    I like the way that you explain ,whenever i got problem in android concept and implementation i check whether you put something about it on RUclips or not. thank you very much

  • @Codetutor-DemystifyCoding
    @Codetutor-DemystifyCoding 5 лет назад

    Any Android developer worth his/her salt will really appreciate coroutines. This is going to make our life much more enjoyable as Android developer.

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

    You are awesome and main thing with every video is you zoom the most important part which I liked the most. Also you explain in layman terms where one can easily understand and implement. Thank you so much this video tutorial helped me a lot.

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

    Thank you, I have gotten confused about coroutines until I find this video.

  • @ahaha233
    @ahaha233 5 лет назад +1

    You're the best Android teacher. Thanks so much for your videos.

  • @kenargo
    @kenargo 5 лет назад +1

    I don't know if you mention it in another Coroutine presentation but there is one other difference between CoroutineScope() and withContext(). CoroutineScope() returns a job object withContext() returns the result of the suspend function. So where you would need to use job::invokeOnCompletion() in order to access results you would be able to direction access the result using withContext()

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

    Never understood Coroutines much better, thank you for the video.

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

    Thanks for the very clear explanation - the best short video I've seen on the subject. Everything explained and no magical coding.

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

    A big thumbs up for making it so simple to understand.
    Love from a small developer.

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

    I'm now super excited to browse through all your content, you kept it really simple and explained the concept quite well, thank you! And subscribed.

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

    First time watching one of your videos. Been working in Kotlin for awhile, but completely new to Coroutines. Nice beginner introduction.
    Would be interested in viewing a series on Coroutines, i.e., how to cancel them, etc. Maybe even something on Flow, etc.

  • @leonardosibela
    @leonardosibela 3 года назад +1

    Quick and interesting question:
    On 18:37 you say that withContext switch the context of the coroutine. Does it actually do that or it changes the coroutines itself?

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

    Excellent introduction to Kotlin and coroutines. You need to already understand threading.

  • @jesprotech
    @jesprotech 3 года назад +3

    I really like this tutorial. Very easy to understand and it cleared up a lot of questions I had. 👍. It really puts in perspective the actual difference between coroutines and threads, which is so important when making reactive and resilient software applications.Thank you!

  • @TiagoDvl
    @TiagoDvl 5 лет назад +1

    Thank you! Simple and without long useless speeches!
    Amazing work, man.

  • @maXXik1337
    @maXXik1337 4 года назад +9

    Me: Simple explanation? 23 minues? Naaah.
    Also me 23 minutes later: Well, good job.

  • @codinginflow
    @codinginflow 5 лет назад +70

    I don't like how you talk about AsyncTask and SQLiteOpenHelper

    • @codingwithmitch
      @codingwithmitch  5 лет назад +54

      They're poop

    • @pembatamang8233
      @pembatamang8233 5 лет назад +4

      ​@@codingwithmitch yes they are 💩

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

      @@pembatamang8233 Also now the AsyncTask is deprecated.

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

      Someone is getting offended

    • @kunaljaiswal7119
      @kunaljaiswal7119 4 года назад +4

      You both are so AWESOME :-)

  • @ngoquang2708
    @ngoquang2708 5 лет назад +2

    Been waiting for this topic for so long. Now I am happy!

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

    Corutines haveb been confusing to me until today. Thanks a lot.

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

    Explained with a very detailed description. Thanks for the video

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

    I really hate Kotlin Coroutines and prefer RxKotlin/Java (Reactive Programming), But this video does a good job of explaining and making it more interesting. I use Coroutines for Ktor only.

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

    Really good introduction to coroutines. Thanks Mitch! Btw, nice t-shirt :)

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

    I've understood everything. This is an excellent video!

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

    I was confused about 22:12 part. Like how does it _wait_ for result1 and then goes to result 2.
    So I looked in to _delay(time)_ method. What happens is that the delay method pauses the execution of the coroutine in which it is called for the given time, and then resumes it. Keep in mind that the coroutine is not a thread, which is why delay causes only the coroutine to pause and not the thread in which it is running.

  • @JanM457
    @JanM457 3 года назад +1

    Thanks for the great explanation!
    Simple, somewhat unrelated question that I am too dumb to google apparently: How are you setting the onClickListener for the button without linking the button through findViewById first? Am I missing something obvious?

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

    Android owes u a lot man...Great series...

  • @hritikgupta189
    @hritikgupta189 5 лет назад +2

    Thanks for keeping us updated !

  • @ofpcwcef
    @ofpcwcef 5 лет назад +1

    You are awesome, please don't stop making tutorials

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

    Thank you for the Video.. You made Coroutine as easy as easy to understand for a beginner.!!!

  • @nakulsingh9913
    @nakulsingh9913 3 года назад +1

    Nicely explained, simple, and easy to get started.

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

    Hey Mitch, great tutorial, I have one doubt though,
    At 21:26 , the fakeAPIRequest function calls getResult1FromAPI which runs on the background thread, however, we are switching the context in setTextOnMainThread. Won't the getResult2FromApi be called on main because we did not switch the context back?

  • @Pinipon.Selvagem
    @Pinipon.Selvagem 3 года назад

    Thank you for the simple tutorial.
    Wanted to replace AsyncTask, since it is now deprecated, and this seems great! It will actually look better than the code i had.

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

    I absolutely loved your lesson on Coroutines. Thank you Mitch

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

    the best video for a beginner. Thank you Mitch.

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

    Thanks a lot this is the most clear and obvious explanation for Coroutines.

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

    Thanks Mitch for explanation. Could you please make videos on Coroutines channel and Flow? I see very few videos out there explaining these concepts.

  • @amitozsingh5992
    @amitozsingh5992 5 лет назад +4

    Please upload more on coroutines

  • @MrEEEE
    @MrEEEE 5 лет назад +2

    Great job Mitch 😍

  • @AhmedAli-jx9ie
    @AhmedAli-jx9ie 5 лет назад +4

    thumbs up before watching the video

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

    i feel like im late to the party, but hey, this intro was really good! Thanks!

  • @karan548
    @karan548 5 лет назад +4

    hey mitch please upload more examples of different cases of coroutines

    • @RameshK-dy6yb
      @RameshK-dy6yb 4 года назад

      may refer ruclips.net/video/x2QOzdh0Fqs/видео.html

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

    i was stuck with an App that i had to use an API requests, you've saved me thank you

  • @alirzayucefaydal591
    @alirzayucefaydal591 5 лет назад

    i shifted from java to kotlin bc of this. i couldnt handle rxjava :D you convinced me :D

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

    Nice Keep on updating new videos. Here in India lockdown till 14 April. hope we learn in meantime from your videos

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

      I'm also giving 20% off my Founding Member subscription until March 29. codingwithmitch.com/enroll/

  • @baijusharma6027
    @baijusharma6027 5 лет назад +3

    Thank you. Today, I learn new thing in Android.

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

    Wow your example is so simple and clear for me, I understand now
    Thank you!

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

    I saw other tuts, this one made it so easy to understand

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

    Beautifully explained.... Sir you've got an Instant sub 😍

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

    thank you for making it simple, i thought i could never learn coroutines.......

  • @carloszetula2307
    @carloszetula2307 5 лет назад

    Thank you so much for your so didactic explanation about Coroutines. From now on I will be able to use it much better way. Thanks a lot again.

  • @AnilKumar-hf2po
    @AnilKumar-hf2po 4 года назад

    Thanks for the Easy understanding of Coroutine. I am expect more from you

  • @samirgawas
    @samirgawas 5 лет назад +2

    Thanks Mitch...waiting for more videos...

  • @TheDaple
    @TheDaple 3 года назад +1

    Thanks, this has really been a nice lesson on coroutines. As this is my first encounter with kotlin and coroutines I am wondering what's the difference (sth. happening in the background) choosing IO or default...

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

    I almost copied your code... but the app didn't crash as expected in 15:31. It wait 1 second to to update the textview.
    weird...

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

    For me learning android concepts means two channels:
    1. Coding with Mitch
    2. Coding in Flow

  • @fatemacherif5069
    @fatemacherif5069 5 лет назад +2

    Thank you so much for this video, Helped a beginner like me a lot!

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

    So simple and soft, thank u a good man for this lesson, I finaly gotcha

  • @bYTeHeaD
    @bYTeHeaD 5 лет назад +1

    Wow, this is indeed very simple to follow.

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

    Hi Mitch, I used your method of using Coroutines to implement a timer, for short duration it was accurate but for longer duration lets say 30 minutes it was lagging behind by 4 seconds

  • @prashant558908
    @prashant558908 5 лет назад

    Amazing video Mitch, great work.

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

    Nice example, now I can continue the Kotlin Adventure... C++ is still easier with threads

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

    A much better explanation than on developer.android.com, where they included the ViewModel and distributed the code on multiple classes. Thanks!

    • @cbnewham5633
      @cbnewham5633 3 года назад +1

      Anything by Google is always over-engineered and explained in the most opaque way possible!

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

    Dear Mitch ,can you please teach us delegation and delegated properties in kotlin?

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

    Excellent video, very usefull, regards from Caracas, Venezuela

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

    Thank you so much! It cleared my mind. Now I know better what is a coroutine!

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

    Well done!!! Really liked how you did this!

  • @jitendrakumar3385
    @jitendrakumar3385 5 лет назад +1

    Please make video on getting current location in activity and fragment (mvvm)

    • @codingwithmitch
      @codingwithmitch  5 лет назад

      You can just use location manager developer.android.com/reference/kotlin/android/location/LocationManager
      Whatever architecture is fine

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

    5:18 : The explanation of suspend keyword seems inaccurate.
    You said the suspend keyword means the function can be used in a coroutine, and if we remove suspend then you can't use it in a coroutine.
    I think it should be that, suspend means the function can ONLY be used in a coroutine, and if we remove suspend then it becomes a normal function, and can be called from anywhere (coroutine or not)

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

    Great tutorial, it's easy to understand and well explained

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

    Thanks for the great video, beginner friendly and practical

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

    I was just need that! thank you for the excelent explanation, best regards

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

    A perfect example. Thank you so much!

  • @jimmyleo5414
    @jimmyleo5414 5 лет назад +1

    Nice speaker , nice illustration.

  • @akhileshshivkumar3464
    @akhileshshivkumar3464 5 лет назад

    Thanks a lot for this tutorial.
    Want to know on which thread getResult2FromApi is getting executed. As you mentioned coroutines would navigate to the thread mentioned in the CoroutineScope. Just before invoking getResult2FromApi, main thread resumed for setting result of getResult1FromApi.

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

    Good job. Well explained easily better than any other source including the awful Kotlinlang.org site