Be VERY Careful with GlobalScope

Поділитися
Вставка
  • Опубліковано 22 лип 2024
  • The best Android courses in the world: codingwithmitch.com/
    There's a few things you should know about Kotlin Coroutines GlobalScope.
    This is the default behavior of coroutines that do not use the GlobalScope.
    1. If the parent is canceled, all the children are canceled.
    2. A parent will always wait for completion of its children.
    If a coroutine is launched with the GlobalScope, those two properties no longer hold true.
    Instagram: / codingwithmitch
    Twitter: / mitch_tabian
    .
    .
    #codingwithmitch
  • Наука та технологія

КОМЕНТАРІ • 55

  • @BelokonRoman
    @BelokonRoman 4 роки тому +13

    Hi,
    the problem you show in the video is not the problem of the GlobalScope. It is the problem of breaking "structured concurrency". If you replace GlobalScope with any another scope(for instance you can write CoroutineScope(Main).launch instead of GlobalScope.launch) - you will have the same issue. This is because you launch new coroutines on the other scope and break parent-child relation

  • @GakisStylianos
    @GakisStylianos 4 роки тому +12

    You're not my dad. I do what I want 🤣

  • @andrefilgueiras
    @andrefilgueiras 4 роки тому +2

    I was using GlobalScope to send some logs to a server and I saw everybody saying not to use it. Now I know that I was right because this is the perfect example to use it. Thanks Mitch, just like you said, GlobalScope is for analytics.

  • @sherazali6324
    @sherazali6324 4 роки тому +1

    @CodingWithMitch I love the content you create please keep up the good stuff like this.

  • @j2shoes288
    @j2shoes288 4 роки тому

    I think you've up'd your game, and giving quality videos. Cheers. ( I've already joined some other course, otherwise, I would join your course ). Maybe I might do so, anyway, this lockdown ( and hence lack of contracting dev roles ) means, I've had time to actually watch some of these videos.

  • @codinginflow
    @codinginflow 4 роки тому +24

    wow!

    • @nicolucian6604
      @nicolucian6604 2 роки тому

      you prolly dont give a shit but does anybody know of a way to get back into an Instagram account??
      I stupidly lost the login password. I love any tips you can give me.

    • @onyxshepard5057
      @onyxshepard5057 2 роки тому

      @Nico Lucian instablaster =)

    • @nicolucian6604
      @nicolucian6604 2 роки тому

      @Onyx Shepard Thanks so much for your reply. I found the site through google and I'm in the hacking process now.
      Seems to take a while so I will get back to you later with my results.

  • @BRUHItsABunny
    @BRUHItsABunny 4 роки тому +1

    thanks for the demonstration, I wasn't able to find much on this subject a few month ago
    i guess i will refactor GlobalScope out of my code where needed soon

  • @gamiike
    @gamiike 2 роки тому

    Thank you Mitch,
    I was just struggling from handling GlobalScope!
    Best Regard from Japan

  • @mohitsinghrawat503
    @mohitsinghrawat503 4 роки тому

    woow just amazing... never ever got this kind of explanation.. love from India

  • @sifatulhaque6170
    @sifatulhaque6170 4 роки тому +1

    I was looking forward to become a founding member but didn’t see any discount.
    Has the discount promo been removed ?

  • @JeffTycz
    @JeffTycz 4 роки тому +4

    What about doing a video on error handling in coroutines. I would be interested in this topic since it's a little different than you normally would handle exceptions and such

  • @silvahawk
    @silvahawk 3 роки тому

    I remember in some demo video, you used GlobalScope on a showToast() helper method to run Toast on main thread, is that not the recommended way to call run Toast normally?

  • @kingsleyetoka2792
    @kingsleyetoka2792 4 роки тому

    Hello Mitch, Thanks a lot for the videos. Can you make a video on cache invalidation with room or any persistence layer. Am a bit confused on when to fetch new data or use existing data in local cache. Thanks a lot

    • @codingwithmitch
      @codingwithmitch  4 роки тому +1

      Save a timestamp when the data was refreshed. Then after x days refresh it

  • @knoppix20
    @knoppix20 4 роки тому

    Mitch what model of microphone you use?

  • @prakashshukla3558
    @prakashshukla3558 4 роки тому +1

    Thumbnail of the month 🏆

  • @irfanirawansukirman4809
    @irfanirawansukirman4809 3 роки тому

    Thanks mitch 👍

  • @fourcade7851
    @fourcade7851 4 роки тому +1

    Hi, how are you? Could you please make me lesson on recyclerview about multiselect in JAVA
    . Thanks beforehand bro

  • @mkpazon_
    @mkpazon_ 4 роки тому +2

    The GlobalScope.launch has the same behaviour of declaring a nested CoroutineScope isn't it?
    CoroutineScope(Dispatchers.Default).launch {
    CoroutineScope(Dispatchers.Default).launch {
    // has no association with parent
    }
    }

  • @charuwaka1
    @charuwaka1 4 роки тому

    can I use the global scope with delay for this method recyclerView.scrollToPosition(position)?

  • @michaelmutinda2567
    @michaelmutinda2567 3 роки тому

    good job

  • @bjornk.567
    @bjornk.567 3 роки тому +1

    Unfortunately your example is flawed... You could replace both GlobalScope.launch { } calls with CoroutineScope(IO).launch { } calls. The output then is still the same. The reason why it behaves that way is that you actually create a new scope with a new parent here -- whether you use GlobalScope or CoroutineScope.

  • @bahaaka828
    @bahaaka828 4 роки тому

    the best use case for GlobalScope is to send the important data (once a specific event occurs) no matter what will happen to the current flow
    example: Error reporting (the report must be sent in any possible way)

  • @foreverrafs
    @foreverrafs 4 роки тому

    Hello Mitch, How do you detect when a CoroutineWorker has been cancelled from within the doWork method when the work gets cancelled by the WorkManager

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      just add an invokeOnCompletion on the parent job. It will always fire when the job is complete or canceled

    • @foreverrafs
      @foreverrafs 4 роки тому

      @@codingwithmitch I'm launching a suspendCoroutine inside the doWork. It doesn't seem to have an invokeOnCompletion method

  • @omkarpawar1741
    @omkarpawar1741 4 роки тому

    can you make a video to explain the mess around how can we Access Media files using raw paths in Android R , Android deprecated the Uri - DATA / MediaStore.MediaColumns#DATA in Android Q , They suggested to use "context.getContentResolver.openInputStream(Uri)" instead it will work with Android, but there are various Libraries like FFMPeg and others which require direct file access(there is an issue with pipe protocol. for ffmpeg), which was prohibited, in Android Q, then in Android R the announced raw file paths access fro media paths will be allowed, but there is no documentation for that , there is furter confusion how would this Affect Android Q and the previous versions, its the strangest development around Android , can you please explain

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      Files and accessing them on android has always been such a MESS. This is something I've stayed away from. There's so many edge cases and weird "one off" rules. It's not fun and very confusing.
      Maybe in the future but no plans right now.

  • @kgandkg
    @kgandkg 4 роки тому +2

    Damn I learnt this today. 🙏. Also be very careful with Fonts.

  • @aabhishek4911
    @aabhishek4911 4 роки тому

    Hi , just out of curiosity , are all of your coroutines videos in a course ? or are you planning something like that ? or they are all independent of each other

    • @codingwithmitch
      @codingwithmitch  4 роки тому +1

      Nah just adding to a youtube playlist

    • @aabhishek4911
      @aabhishek4911 4 роки тому

      @@codingwithmitch Great , can i get the link to the playlist :)

    • @codingwithmitch
      @codingwithmitch  4 роки тому

      @@aabhishek4911 Look on my channel

    • @aabhishek4911
      @aabhishek4911 4 роки тому

      @@codingwithmitch , Thanks , On a different note , in your course Powerful android apps with architecture , are you using data binding ? from the overview it does not look like that , if not using any reason for that ?

  • @bayraminanc885
    @bayraminanc885 4 роки тому

    hi,
    laucn{
    .....
    }
    laucn{
    .....
    }
    Does this code snipped when running in courotine, is it working sequantially or paralel?

  • @wattheshet
    @wattheshet 4 роки тому

    just a feedback on your site. Turns out you have to login when choosing to become a member. I tried to become a member as guest did not work. You should do something about that or show a dialog let the want to be member know.

    • @codingwithmitch
      @codingwithmitch  4 роки тому +1

      Always working on the website. Finding the time is difficult.

    • @wattheshet
      @wattheshet 4 роки тому +1

      @@codingwithmitch Focus on that. That's where you get money :D

  • @andy_lamax
    @andy_lamax 4 роки тому

    Hi, You have good content. But I am sorry to say that GlobalScope.launch is not the same as CoroutineScope(Main).launch(Dispatchers.Default) as stated at 6:23. Yes, the GlobalScope uses the default dispatcher internally, but know that you can even use the GlobalScope to launch coroutines in the main thread. i.e. GlobalScope.launch(Dispatchers.Main). Thats not how structured concurrency works. GlobalScope just means, everything will be launch in the global scope as a child.

    • @codingwithmitch
      @codingwithmitch  4 роки тому +2

      Yes I think the way I said that was confusing. The point I was trying to make was GlobalScope will pull from the same thread pool as Dispatchers.Default. That's all.

  • @tarlamas
    @tarlamas 4 роки тому +1

    Neat explanation! GJ
    GJ= Good Job not GlobalScope Job :P

  • @ChrisAthanas
    @ChrisAthanas 3 роки тому

    Engagement

  • @oscarperry641
    @oscarperry641 4 роки тому

    LIKE!

  • @bagadeshkumarr9502
    @bagadeshkumarr9502 3 роки тому

    Sorry mitch. That is not an issue with global scope. Issue is structured concurrency.

  • @HienNguyen-xn7tr
    @HienNguyen-xn7tr 4 роки тому

    now im always careful of globalscope

  • @gofudgeyourselves9024
    @gofudgeyourselves9024 4 роки тому

    Hey mithc dont you get bored of doing Android Development daily. Are you exploring new technologies.

    • @codingwithmitch
      @codingwithmitch  4 роки тому +1

      Pretty hard to get bored with android when there is so much stuff constantly.
      I'm always "looking" at new stuff. But I only dive into things I'm really interested in. Like Python.

  • @francescomarzo307
    @francescomarzo307 4 роки тому

    That's the answer at the question everybody wants to know...Lebron, what's your decision?
    ua-cam.com/video/RTeCc8jy7FI/v-deo.html