Don't Make These 2 Mistakes With Lists in Kotlin

2023 ж. 12 Қыр.
13 368 Рет қаралды

In this video you'll learn about 2 mistakes you can easily make when using lists in Kotlin.
LAST chance to apply for a spot in my 10-week Android mentoring program to 10x your skills:
pl-coding.com/drop-table-ment...

Пікірлер
  • Would be good to mention the more "Kotliny" way of locking by using a mutex, since technically locking means blocking the thread, and also synchronized will not work when inside a coroutine.

    @xybnedasdd2930@xybnedasdd29308 ай бұрын
  • Good video, however i don't think the issue presented here is specific to lists, race conditions can also happen with any kind of mutable data.

    @saabzero@saabzero8 ай бұрын
    • True :)

      @PhilippLackner@PhilippLackner8 ай бұрын
    • But collections in particular - that's a textbook example. Although a collection doesn't have to be a List. In fact, this example really calls for a Map, since the messages are indexed - looking an index up with indexOfFirst every time is a bit iffy. Could be a LinkedHashMap, if the order matters. I know it was only meant to illustrate the concept; consider it as a side remark, not criticism

      @vibovitold@vibovitold8 ай бұрын
  • hi Philipp, i would like to see a video about your story. How you became developer, how you got better and became such knowledgeable and proficient. Something about your mindset, your daily routine when you were at the beginning, 3 years into coding, 7 years and now. Thank you for everything you do :)

    @andi6271@andi62718 ай бұрын
  • Love this time format. More please!

    @toyboatt@toyboatt8 ай бұрын
  • Дякую за чудове пояснення.

    @michaels891@michaels8918 ай бұрын
  • Thanks Philipp, great content!! is it there a way to test the case when the race conditions happen?

    @manuelsalas6190@manuelsalas61908 ай бұрын
  • thanks for sharing

    @Benverge@Benverge8 ай бұрын
  • I think a critical race condition that would break list-indices in the first Chatmanager example-code is only theoretical unless you add code to delete messages or change the order of messages in the list. Right now with only adding, indices are stable and working on them with multiple threads should not introduce any bug. Please correct me if I am wrong (but please give a reason and explain).

    @StingerAJ@StingerAJ8 ай бұрын
    • I agree since adding to the list will only append. Indices will break if messages are inserted by index and also by removed as you mentioned. So the first chat manager looks fine.

      @user-nh4fd8eb5c@user-nh4fd8eb5c8 ай бұрын
  • Hi Philipp. This is very interesting. I would have liked, if you run the code to see exactly where race conditions are at work. Maybe a follow up tutorial?

    @lindaporsius@lindaporsius8 ай бұрын
  • This is a good idea although synchronization comes with performance cost in increased latency. Theres also possibilities of deadlocks and resource contention. I'll be working on a small personal project to see how dealing with this in the real sense gets.

    @cazewonda@cazewonda8 ай бұрын
  • In this case if you're only ever adding to the end of the list and we're using a unique ID to find the index, then the index will always refer to same element, won't it? If there is no other code modifying the list I don't think we'd see a bug. A race condition that results in a bug could occur if we were moving elements around or deleting elements. Edit: I see that @StingerAJ pointed out the same thing.

    @KCIsMe@KCIsMe8 ай бұрын
  • Thank you, for video. I think you could also mention about CopyOnWriteArrayList, that preferable for this kind of operations )

    @user-eo9gb1qq7n@user-eo9gb1qq7n8 ай бұрын
  • Can you also show how coroutine channels can be used to synchronize changes as well?

    @robchr@robchr8 ай бұрын
  • Good video, and is the main reason why I use Mutex objects to lock on coroutines

    @507cHoco@507cHoco8 ай бұрын
  • I see huge progress in the insertion of advertising content

    @bro_chenzox@bro_chenzox8 ай бұрын
  • Content we pay our internet bill for :)

    @safionweb@safionweb8 ай бұрын
  • I'm confused about why the index in `onMessageDeliveryStateChanged` would not be valid if all we are doing is adding to the message list. Are indices reevaluated every time a list is updated?

    @byaruhaf@byaruhaf8 ай бұрын
  • Hello, would you turn on "Thank you" option as well so we can contribute to your videos as well ? thanks :)

    @saeedghiassy5435@saeedghiassy54358 ай бұрын
  • What theme you use ?

    @oscarescam@oscarescam8 ай бұрын
  • Good

    @dandamiano@dandamiano8 ай бұрын
  • will this method work for methods marked as suspend? I think it's better to use Muxed

    @ddstudio1580@ddstudio15808 ай бұрын
  • Why not use volatile annotation over the messages list?

    @ejtravel3034@ejtravel30348 ай бұрын
  • i would personally go with AtomicReference or Volatile

    @nikolozlatsabidze2196@nikolozlatsabidze21968 ай бұрын
  • I use kotlins Mutex concept not javas syncronized in that case but both should work ❤

    @addyi89@addyi898 ай бұрын
    • But Muted would mean you have to use suspend funs

      @mark-147@mark-1478 ай бұрын
    • true but that is my many case that courses concurrency issues.

      @addyi89@addyi898 ай бұрын
  • Question regarding the synchronized. Does it not execute or add the execution in queue?

    @zeeshansyed4654@zeeshansyed46545 ай бұрын
  • Sir, Android 11 factory reset data can recover?

    @user-sw5zk2vd7r@user-sw5zk2vd7r8 ай бұрын
  • whats your android studio theme?

    @koweratus@koweratus8 ай бұрын
  • Is this going to work if you were using coroutines?

    @vahekhachaturian2424@vahekhachaturian24248 ай бұрын
    • No, it won't work with coroutines, but you can find corresponding manual in official Kotlin docs named "Shared mutable state and concurrency".

      @Denis0Kor@Denis0Kor8 ай бұрын
  • wouldn't be better to use a State Flow instead or a Shared State Flow.

    @manuelsilveriof@manuelsilveriof8 ай бұрын
  • Hi Phillip, idk what happened in the last videos but the audio quality dropped, your voice sounds a little bit weird

    @Carlos-xz9zq@Carlos-xz9zq8 ай бұрын
    • Thank you I will check that with my editor

      @PhilippLackner@PhilippLackner8 ай бұрын
  • goat

    @icodethis@icodethis8 ай бұрын
  • synchronized(this) should also do the job

    @sebastiankupis4448@sebastiankupis44487 ай бұрын
  • I would avoid synchronized with Kotlin.

    @pqsk@pqsk8 ай бұрын
  • Philipp you are doing great content but something dies inside of me when I see those thumbnails >_

    @antonkorieniev8691@antonkorieniev86918 ай бұрын
    • hahaha

      @salmanjawed98@salmanjawed988 ай бұрын
  • Deadlock...

    @m77mo65@m77mo657 ай бұрын
KZhead