95% Don't Understand THIS About Coroutines (Main-Safety)

Поділитися
Вставка
  • Опубліковано 18 гру 2024

КОМЕНТАРІ • 80

  • @scottloewke5199
    @scottloewke5199 10 місяців тому +16

    The rule Philipp refers to is a convention only. It states that a suspend function should never block the calling thread. If we write the suspend function, then the responsibility is ours to ensure that it is main-safe. If we use a 3rd-party suspend function, then it's the responsibility of that developer to abide by the convention. However, it is still our responsibility to ensure (e.g., read the documentation or study the code) that the 3rd-party suspend function follows the convention. In the case of both Retrofit and Room, their documentation states that any suspend functions they generate will be main-safe.

  • @bitwisedevs469
    @bitwisedevs469 Рік тому +16

    Always setting the Dispatcher even though we shouldn't is a code smell. Thanks for awesome content

  • @hamedborna7037
    @hamedborna7037 Рік тому +8

    Thanks Philipp it was very useful. to me you're the most trustworthy person I've ever seen. I can always rely on your content🤘🏻

  • @bidbid5413
    @bidbid5413 Рік тому +7

    Thank you for your videos Philipp! I always learn something new!

  • @princeneo294
    @princeneo294 Рік тому +3

    Philipp doing what Philipp does, Thanks

  • @eugenepopovich2264
    @eugenepopovich2264 Рік тому +4

    You should not catch generic exception in the suspend method, such as coroutine may throw `CancellationException` to interrupt invocation

    • @PhilippLackner
      @PhilippLackner  Рік тому +4

      Yes I covered that in a separate video, but I thought not to further overcomplicate this one with an additional non-trivial concept :)

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

      If your try-catch block can catch a cancellation exception, then the code is already written incorrectly. The block must catch code exceptions inside the corroutine builder or the block must wrap the topmost corroutine builder - the parent.

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

      @@PhilippLackner What is the video?

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

    Really useful. Thank you Philipp. Please more videos like this.

  • @GS-wf1cp
    @GS-wf1cp 6 місяців тому

    Mate I can't explain how much I love your content

  • @juanherrera9521
    @juanherrera9521 5 місяців тому

    withContext just means that everything inside its lambda will be executed in the specified dispatcher, it does no mean that it will magically make the functions called inside of it non-blocking, but it is useful because we can offload the main thread from heavy operations (like an http call or a filesystem operation) and just block a thread on the IO dispatcher instead and then get the result on the calling thread (Main in this case), so it is basically a callback but without the ugliness of it

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

    interesting contents! It was useful tips. Especially 'suspend function call' icon in Android Studio. Thanks philipp

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

    You can use repeat(5) {} instead of that for

  • @bigman2127
    @bigman2127 Рік тому +5

    I agree with all the points provided, but what if a repository method does large mapping stuff / CPU work / multiple calls to Room's DAO? Should i wrap a method with withContext(...) or put withContext(...) inside mappers and DAOs?

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

      If mapping is CPU heavy, the mapping function should switch the context to the default dispatcher

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

      @@PhilippLackner why not computation dispatcher?

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

      @@Coden55 sorry, typo I correct it. Of course the default one

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

      There is no such thing as light computation on UI thread. Any computation on it will result in a frame drop, it just a question when and how many frames will be dropped.

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

    Thanks for the info!

  • @Hmmm964
    @Hmmm964 Рік тому +2

    Hey, how does retrofit switch to the background thread? Is there any official source that declares that?

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

    Thank you Philipp, your videos are very valuable to us.🎉

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

    Great video again :3

  • @BharadwajRavinuthala
    @BharadwajRavinuthala Рік тому +15

    Philip woke up and decided to show the facts 😅

  • @marianpazdzioch5437
    @marianpazdzioch5437 11 місяців тому

    6:43 "even if it would switch to main dispatcher this would not be a problem" how come?

  • @GabrielTrifa
    @GabrielTrifa 10 місяців тому

    excellent video!

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

    Very useful and trusted

  • @TravelVideos1234
    @TravelVideos1234 Рік тому +5

    One of the thing to keep in mind is that you should be injecting the dispatcher. That way you can inject test dispatcher while writing unit tests

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

    Amazing content as usual, Consider making a video on how to retrieve data from a suspend function via flow or callback functions.

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

    i've been researching coroutine context for the past 5 days 😮

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

    Thanks phlipp for this useful video. I want to ask a question about this topic. I have a countdown timer in whole app that in activity onCreate. This timer is resetting at user interaction. This timer must be in io thread? Is it necessary?

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

    Hey Phil, do some magic on compose UI!

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

    I faced this problem. Thanks for the detailed video 😍

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

    Can you change that yellow lamp to green one 💚

  • @Revitalish
    @Revitalish 7 місяців тому

    it wasn't that clear..
    so the coroutine scope should only be written inside a suspend function? not from the repo's function and not from the ViewModel where you call the repo function?

  • @alexbecar977
    @alexbecar977 Рік тому +2

    I'm a little confused sorry, when you say that that the non suspending blocking part of a corutine won't block the underlying thread you mean it won't block the main ui thread right? The IO thread on which the corutine runs on will be blocked by the synchronous code in the coroutine until the coroutine reaches a suspending point?

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

      Not the whole thread will be blocked, just the single coroutine. You can launch lots of Coroutines on the same thread which will all run independently

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

      @@PhilippLackner Thanks a lot

    • @marianpazdzioch5437
      @marianpazdzioch5437 11 місяців тому

      ​@@PhilippLacknerindependently? You mean concurrently or parallel?

    • @Pavel-wj7gy
      @Pavel-wj7gy 4 місяці тому

      @@marianpazdzioch5437 Concurrently, not in parallel. Parallel run implies using multiple CPU cores, not threads.

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

    Why try-catch in the Repository? Adding this error handling logic should be located in the Use Case

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

    When designing a public api, what would be the best approach to let the user know that he don't need to use with Context? Or Public api should be always mainsafe?

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

      That's the concept, the user should be able to rely on your API properly switching the dispatcher

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

    @philip please tell me why jetpack compose fell laggy please one vadeos on this

  • @Daaaaaaavid
    @Daaaaaaavid Рік тому +2

    Coroutines and supend functions are the most difficult thing to me to understand since Android 1.x

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

    Is there any downside if we wrap a suspending function with a with context(someDispatcher) anyway? For example: I've a room DAO and in the repository implementation, I wrap my dao call with a with context() block despite knowing room calls are done in background thread.

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

      Not a performance downside, but overdoing this makes the code less readable since every withContext adds another indentation and line of code

  • @dvg-in-eu
    @dvg-in-eu Рік тому

    But is it a good idea to give this control to retrofit ? I mean, it's retrofit and yes we can trust it, but what we are using other libs which are fetching or doing something isn't it better for us to do this change ourselves?

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

    So, this information is really useful, however, if every suspend function is supposed to be Main Safe, then why should we not switch off of the main thread ASAP and onto the default thread? After all, if the Main thread is for doing UI work, then why do non-UI work on it?

    • @marianpazdzioch5437
      @marianpazdzioch5437 11 місяців тому

      Because it would be a nightmare from maintenance of that code point of view.
      Technically you could switch every list iteration to non-main thread but in reality nobody does that.

  • @SerhiiSolodilov
    @SerhiiSolodilov Рік тому +3

    Tightening yourself to other library implementations isn't a good recommendation.

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

      A small use case that could lead to blocking code inside the library would mess the app up. I agree with you, even though his video is on point.

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

    Nice One

  • @Abdulrahman-uk9ny
    @Abdulrahman-uk9ny Рік тому

    Can you please record your videos in 4k?

  • @radosawjuszczyk638
    @radosawjuszczyk638 8 місяців тому +1

    seeing `for(i in 1 .. 5){}` instead of `repeat(5) {}` hurts my eyes :

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

    i did not get the point.. please

  • @tegarsubkhanfauzi4641
    @tegarsubkhanfauzi4641 Рік тому +2

    Please make a tutorial how to build android tv with jetpack compose.. thaanks ❤❤❤

  • @Naxomiun
    @Naxomiun Рік тому +4

    For the people out there: This does not mean that you should not take care of your threads just because there is a call to network through retrofit at the end of your logic. You would end up executing your entire code at the scope default dispatcher; probably Main.Inmediate.

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

      this is absurd situation you imagined
      people do not normally put all blocking logic inside one function terminated with retrofit call.
      you should have your blocking code in separate main-safety functions

    • @Naxomiun
      @Naxomiun Рік тому +2

      ​​​@@trollberserker1515what? Its really common to map data models into domain/ui models, merge lists from two endpoints, or just formatting some info before sending it to an endpoint. If you dont handle your threads you'll execute all this logic in your main thread. Its simple to understand. Retrofit switches your dispatcher right before making a net call.

    • @marianpazdzioch5437
      @marianpazdzioch5437 11 місяців тому

      Still, if those lists are not long (arbitrary, YMMV) then mapping/iterating over them on main thread is acceptable.

  • @Shubhamkumar-ee8mi
    @Shubhamkumar-ee8mi Рік тому +1

    Thank you a lot It will really help me
    But i have one doubt because before that I always switch context to IO in repository.
    It means we dont need to switch context to IO when making http request using retrofit ? Yes or No and explain please

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

    Holy cow, I am one of those who used it incorrectly.

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

    I have a doubt when making a splashscreen when producing on my cell phone, it installs 2 apps, one with the splashscreen and the other with the normal one. And if I uninstall one, both are gone

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

    Brother teach us backend as well apart from firebase

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

    Now i know why my code is lagging

  • @George-q4z
    @George-q4z Рік тому

    😃

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