Is it okay to use runBlocking?

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

КОМЕНТАРІ • 21

  • @eugenepopovich2264
    @eugenepopovich2264 9 місяців тому +33

    `runTest` should be used in unit tests which uses `TestScheduler` under the hood

    • @PhilippLackner
      @PhilippLackner  9 місяців тому +2

      Yes true thanks for adding! doesn't make a difference in the example I showed though

    • @eugenepopovich2264
      @eugenepopovich2264 9 місяців тому +3

      Thanks for the reply. Your channel has a very wide audience and many inexperienced watchers may think that using runBlocking in unit tests is a best practice, while it is not, unfortunately. So just thought it is worth mentioning that there is an alternative way designed specifically for the case.

    • @PhilippLackner
      @PhilippLackner  9 місяців тому

      @@eugenepopovich2264 the only real difference is that runTest skips delays, which is not often needed. Apart from that they behave pretty much the same. Also with runBlocking, you can use a test dispatcher

    • @OniBojan
      @OniBojan 9 місяців тому +6

      @@PhilippLacknerI would not agre. We had a lots of issues with flaky tests in our company's project. Switching back to runTests, resolved some of the flakines.

  • @OniBojan
    @OniBojan 9 місяців тому +11

    For tests, we should use runTests, since it uses testScheduler. Using runBlocking can lead to flaky tests.

  • @gekylafas
    @gekylafas 9 місяців тому +1

    Another valid use case for `runBlocking` I have found is inside `Filter.performFiltering()` (if you are so unlucky and still have to use Adapters in your app). Just like the Okhttp interceptor example, `performFiltering()` is called on a different thread than main, so you can block it without freezing the UI.

  • @BenKadel
    @BenKadel 9 місяців тому +1

    Ooooooo new thumbnail style! I like it!

  • @mohancena8970
    @mohancena8970 9 місяців тому +2

    Hey! Philipp you forget add this video to coroutines playlist 😅

  • @MobileMastersAndroidiOS
    @MobileMastersAndroidiOS 9 місяців тому +3

    When writing unit tests for code that involves suspending functions or coroutines, runBlocking helps you to create a structured way to run asynchronous code in a synchronous testing environment.

  • @TVHovna
    @TVHovna 9 місяців тому +1

    You forgot to mention that runBlocking can throw InterruptedException and may cause additional weird behavior (check open issues)

  • @universe8989
    @universe8989 9 місяців тому

    U always know what I need rn

  • @hardendurorider
    @hardendurorider 9 місяців тому

    i normally only use them when I have to switch from suspend functions to some older rxJava stuff(assuming is non ui thread) and i don't have time to refactor

  • @jedikv
    @jedikv 9 місяців тому

    Good video. Isn't runblocking also used for desktop main functions? I see it whenever I create a new project on Intellj

  • @voidpointer398
    @voidpointer398 9 місяців тому

    Can't I user CoroutineScope in interceptor?

  • @mitulsheth
    @mitulsheth 9 місяців тому

    helpful ......thank you

  • @ALEX54402
    @ALEX54402 9 місяців тому

    Please add a video about how i optimise my codes

  • @shalenmathew980
    @shalenmathew980 9 місяців тому

    Can u make videos on some random projects like u used to back then

  • @ALEX54402
    @ALEX54402 9 місяців тому

    ❤❤❤❤❤🎉🎉🎉

  • @hrc2514
    @hrc2514 9 місяців тому +3

    On real project run blocking is not recommended only for testing

    • @mubaraknative
      @mubaraknative 9 місяців тому

      Yes, When we have a usecases if we want to test the Coroutines behaviour currently stop our current thread it will help us!!