How to avoid 5 common Kotlin Coroutine mistakes

Поділитися
Вставка
  • Опубліковано 22 лип 2024
  • 🎓 Get the full 15+ hour-long Course "Kotlin Coroutines and Flow for Android Development" with a nice discount here: www.lukaslechner.com/coroutin...
    In this video, I am going to show you 5 of the most common mistakes when using Kotlin Coroutines, so that you don’t make them in your applications.
    👨‍🎓 Learn Coroutines and Flow with my Github Example Repository: github.com/LukasLechnerDev/Ko...
    TimeStamps:
    00:00 - Intro
    00:36 - Mistake 1 - Unnecessary Dispatcher Switches
    03:36 - Mistake 2 - Not supporting Cancellation
    07:59 - Mistake 3 - Using try/catch for exceptions within Coroutines
    10:54 - Mistake 4 - Install Coroutine Exception Handler in Child Coroutine
    11:36 - Mistake 5 - Accidentally catching CancellationExceptions
    15:29 - Full Course "Mastering Kotlin Coroutines for Android Development"
    ➤ Subscribe to the channel: / @lukaslechner
    🌎 Check out my website: www.lukaslechner.com
    📬 Subscribe to my newsletter: www.lukaslechner.com/newsletter/
    ➤ Follow me on twitter: / lukaslechnerdev
  • Наука та технологія

КОМЕНТАРІ • 16

  • @lukaslechner
    @lukaslechner  2 роки тому +1

    🎓 Get the full 9+ hour-long Course "Mastering Kotlin Coroutines for Android Development" with a nice discount here: www.udemy.com/course/coroutines-on-android/?couponCode=30D5A70DBF2D173FC4EB

  • @Ygormaniz
    @Ygormaniz 2 роки тому +5

    Really great video, especially for covering Main-Safety. Most of the content that I read doesn't mention that

  • @neerajpant4071
    @neerajpant4071 2 роки тому +1

    The last one was awesome 😎 we usually use try catch , but in coroutine we should avoid that .use either cancellation or handle exception using cancellation exception

  • @jess_o
    @jess_o 2 роки тому +1

    Great video, thanks for succinct demos and solutions!

  • @ykalchev
    @ykalchev 2 роки тому +1

    Amazing video, very helpful. Thanks mate :)

  • @BenKadel
    @BenKadel Рік тому +1

    Excellent Video! Well done so useful!

  • @akashkhunt164
    @akashkhunt164 2 роки тому +1

    Just a minor thing that in "Mistake 4 - Install Coroutine Exception Handler in Child Coroutine" it is not mandatory to install "exceptionHandler" in both CoroutineScope and parent Coroutine, installing in any one of them will do the job :). Nonetheless great video about all the very common pitfalls (especially the last one)

    • @rollebonmarquis7574
      @rollebonmarquis7574 Рік тому

      Maybe it can also be noted that you can have the handler in a child coroutine if this coroutine is a direct child of a supervisor scope and also an explanation as to why does this behave like that but I understand that that much info might cross the boundary of making a video on "common mistakes with coroutines" and enter some other zone. (saying this a layman on this topic)
      And I agree that the video was great and it was of huge help to me.

  • @131617
    @131617 2 роки тому +1

    👏👏👏

  • @cockyboby
    @cockyboby Рік тому

    A little bit confusing. You said 6:21 "like delay, withContext..." so you made statement that 'withContext' is also a suspension point which you use in the example, but it isn't cancelled. Why?

    • @lukaslechner
      @lukaslechner  Рік тому

      withContext will, like delay throw a CancellationException when the Coroutine in which it was called got cancelled and so the Coroutine stops its execution. However this doesn't mean that all code within the withContext block is immediately stopped once the coroutine gets canceled. You need regular suspension points in there so that a CancellationException can be thrown. Does this answer your question?

  • @DF-ss5ep
    @DF-ss5ep 2 роки тому

    Are "suspend" functions "suspending functions"? Is that where that keyword comes from? Is it because they can throw CancellationException?

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

      I made a video about suspend functions, that should answer your question ua-cam.com/video/bCa2ua58EdE/v-deo.html

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

    This video wasn't useful. it was extremely useful. Coroutines are the main part of every Kotlin android project.

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

    Just a little hint. At mistake 3 you're trying to catch a java.lang.Exception but throw a java.lang.Error which is not a subtype of the former. Thus irrespective of the whole suspension, the catch block wouldn't work.
    Great videos otherwise. :)

    • @lukaslechner
      @lukaslechner  2 роки тому +1

      Oh, you are right! Thanks for the hint!