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 - Наука та технологія
🎓 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
Really great video, especially for covering Main-Safety. Most of the content that I read doesn't mention that
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
Great video, thanks for succinct demos and solutions!
Amazing video, very helpful. Thanks mate :)
Excellent Video! Well done so useful!
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)
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.
👏👏👏
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?
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?
Are "suspend" functions "suspending functions"? Is that where that keyword comes from? Is it because they can throw CancellationException?
I made a video about suspend functions, that should answer your question ua-cam.com/video/bCa2ua58EdE/v-deo.html
This video wasn't useful. it was extremely useful. Coroutines are the main part of every Kotlin android project.
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. :)
Oh, you are right! Thanks for the hint!