5 Kotlin Coroutine Secrets I Wish I Knew Earlier

  Рет қаралды 24,778

Philipp Lackner

Philipp Lackner

Күн бұрын

Пікірлер: 46
@laurent.treguier
@laurent.treguier 5 ай бұрын
For secret #3, I think another option if you really need to catch general exceptions could be to catch CancellationException in its own, dedicated catch block to rethrow it. Not much different, but I like the clarity of using separate catch blocks for each type of exception that can be caught
@yurowitz
@yurowitz 5 ай бұрын
Ive 7 years of experience in Android app dev, But I never paid any attention to these! Thank you so much!
@Denis0Kor
@Denis0Kor 5 ай бұрын
note about 5 example, if you read/write data byte-by-byte it could be very slow...
@olgaderyabina7519
@olgaderyabina7519 4 ай бұрын
When I see hardcoded dispatchers in the code, it's usually a sign that people don't care about testing functionality with coroutines in general 🙂
@govindyadav7591
@govindyadav7591 5 ай бұрын
You can use isActive in your while loop that will solve the problem isn't it ? 14:44
@5erTurbo
@5erTurbo 5 ай бұрын
For #4, in case of Dagger, you could use qualifier, and inject Dispatcher directly. Or without DI, simply set it to some default value.
@Radiokot42
@Radiokot42 4 ай бұрын
What if in #3 we do not re-throw, but replace while (true) with while (isActive)?
@ArthurKhazbs
@ArthurKhazbs 5 ай бұрын
The secrets 2 and 5 🤯
@ahmed_guedmioui
@ahmed_guedmioui Ай бұрын
This is a priceless video
@MuhammadBilal-oo4mf
@MuhammadBilal-oo4mf 5 ай бұрын
Great Video! Philipp Lackner
@AbdulelahAGR
@AbdulelahAGR 4 ай бұрын
I have to go check my app for 2 & 3. Thank you. I love learning about Coroutines
@stasleonov5196
@stasleonov5196 5 ай бұрын
Philipp,thanks for the video!
@mr.suryansh
@mr.suryansh 5 ай бұрын
Hey Philipp, Thanks for the video! Could you please make a new video on "How to use Room with KMP"? Previously, your video covered only Android and iOS, but I would like to see how this can be done for desktop as well. Also, it would be great if you could create a full 3-4 hour production ready project based on KMP that runs on Android, iOS, and desktop. Thanks!
@TheAndreArtus
@TheAndreArtus 5 ай бұрын
On tip 3, you can have the exception handling outside the loop (if you want the behaviour of cancelling the loop) i.e. try { while {} }catch {}. This advice may not be pertinent to every exception, but swallowing general exceptions in a loop is rarely advisable.
@PhilippLackner
@PhilippLackner 5 ай бұрын
@@TheAndreArtus that will still break the cancelation mechanism and can execute code that shouldn't be executed
@rootamit
@rootamit 5 ай бұрын
Thanks for such a great video ;)
@atlas6397
@atlas6397 5 ай бұрын
I knew about supervisor, it is used also in viewmodelScope
@jakosss
@jakosss 5 ай бұрын
Please never ever ever ever copy anything byte by byte. You made the function as inefficient as possible. Copy single byte then do ensureActive and your file is copying like 1000 times slower? Always use buffers so such operations start with 4 kb buffer and measure, experiment. But please, never do byte by byte...
@BelokonRoman
@BelokonRoman 4 ай бұрын
I've already wrote this in another comment: I did a benchmark for readBytes and reading byte by byte with 5.2Mb file and the results are: readBytes - 41..58ms, byte by byte - 26.6..27.5 seconds
@K-BAAP
@K-BAAP 2 ай бұрын
@@BelokonRoman really?
@Alchemist10241
@Alchemist10241 5 ай бұрын
I think I've made one mistake relating to secret number 2 (Supervisor Job) in my last project. I have a bug (not a crasher) in the app but it happens rarely.
@bijithbiju6298
@bijithbiju6298 5 ай бұрын
Hi please help me i want to do work in kotlin not jetpack. I want to do data transfer using local only hotspot is that possible
@sebastianpalm3495
@sebastianpalm3495 4 ай бұрын
love it!
@R0bJ86
@R0bJ86 4 ай бұрын
Hi Philipp. I would like to tell you how much your videos like this one helped me in the recent past and I'm very grateful to you because of it. Also I'd like to give you something back: for Secret 1 there is actually a Service implementation that has a built in lifecycle and lifecycleScope just like a viewModel has and it's called LifecycleService.
@princeneo294
@princeneo294 5 ай бұрын
This is awesome
@LightDante
@LightDante Ай бұрын
Yield() or EnsureActive(), which is best practice?
@FilipAdam-v9u
@FilipAdam-v9u 4 ай бұрын
I agree with everything you've read, colleague
@meshoraouf8929
@meshoraouf8929 5 ай бұрын
Wow can I use it in multiplatform kmp or not?
@PhilippLackner
@PhilippLackner 5 ай бұрын
@@meshoraouf8929 Absolutely👍
@ArthurKhazbs
@ArthurKhazbs 5 ай бұрын
Yes, these tips are all features of the KotlinX Coroutines library. The only platform-specific parts of this video were the Android app components lifecycle and the specific file operations.
@213TechCocoon
@213TechCocoon 5 ай бұрын
Fifth solution is not a solution, But new problem
@smallbig857
@smallbig857 4 ай бұрын
Facts about Kotlin that annoy me: - They added null safety. But removed exceptions safety. - They could have built a cancellation mechanism into the generated code. But they invented CancellationException.
@PhpXp
@PhpXp 5 ай бұрын
Secret 5 is going to destroy the performance though ...
@PhilippLackner
@PhilippLackner 5 ай бұрын
You'll be shocked what code you'll find when looking into the toByteArray() function 🤯 TL;DR: No, that won't have an impact on performance, it's the same code with an extra check for cancellation which is nothing else than an extra if-statement being evaluated for each byte. Unless you're running your code on phones from 1995, that won't have an impact ;)
@PhpXp
@PhpXp 5 ай бұрын
@@PhilippLackner I see there's a System.arraycopy(). A slightly redundant array copy. Not great, not terrible. I was working on a project more than 5 years ago, I wrote the same code in some networking part of the app. Changing it to the read(array, position, length) call and copying 1KB at a time improved performance by like a 1000x. It was a noticeable difference. Anyway, Kotlin should handle this better. There should be a standard library call to do this. There is runInterruptible(), but that's not perfect either ...
@PhilippLackner
@PhilippLackner 5 ай бұрын
I don't have any data on this, but you could get rid of that arraycopy() by creating the array on your own and appending each newly read byte in each iteration
@mesutemrecelenk5447
@mesutemrecelenk5447 5 ай бұрын
😂​@@PhilippLackner
@Arcao
@Arcao 5 ай бұрын
Exactly! I would read input stream by chunks or at least wrap input stream to BufferedInputStream. The second thing I would pre-allocate byte array in ByteArrayOutputStream as the readBytes() function do. This prevent internal resizing byte array after adding bytes.
@vanhieuao4154
@vanhieuao4154 4 ай бұрын
plsss speak sloww
@PrathivAR-f7l
@PrathivAR-f7l 5 ай бұрын
Without SupervisorJob() on scope , still other coroutines continues to run val scope = CoroutineScope(Dispatchers.IO) Button(onClick = { scope.launch { launch { try{ println("child 1 coroutine") delay(2.seconds) throw Exception("Failure in Child 1") } catch (e : Exception){ throw CancellationException() } } delay(4.seconds) launch { println("child 2 coroutine") } } }) { Text(text = "Scope check") }
Hot Flows vs. Cold Flows In Kotlin - When to Use What?
14:35
Philipp Lackner
Рет қаралды 18 М.
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
The evil clown plays a prank on the angel
00:39
超人夫妇
Рет қаралды 53 МЛН
Tuna 🍣 ​⁠@patrickzeinali ​⁠@ChefRush
00:48
albert_cancook
Рет қаралды 148 МЛН
人是不能做到吗?#火影忍者 #家人  #佐助
00:20
火影忍者一家
Рет қаралды 20 МЛН
Coroutines: Concurrency in Kotlin
30:22
Dave Leeds
Рет қаралды 22 М.
MVVM vs. MVI - Understand the Difference Once and for All
18:40
Philipp Lackner
Рет қаралды 57 М.
5 Fatal Coroutine Mistakes Nobody Tells You About
18:15
Philipp Lackner
Рет қаралды 90 М.
This Is My FAVORITE Error Handling Class
28:57
Philipp Lackner
Рет қаралды 37 М.
Stop With Software Estimates
16:54
ThePrimeTime
Рет қаралды 151 М.
derivedStateOf VS. remember(key) - THIS is Really the Difference 🤯
14:46
How to avoid 5 common Kotlin Coroutine mistakes
15:59
Lukas Lechner - Advanced Android Development
Рет қаралды 5 М.
FULL Guide to Kotlin's Inline Keyword (inline, crossinline & noinline)
16:35
How principled coders outperform the competition
11:11
Coderized
Рет қаралды 1,8 МЛН
Kotlin Coroutines 101 - Android Conference Talks
24:49
Android Developers
Рет қаралды 138 М.
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН