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 - Наука та технологія
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
You're not my dad. I do what I want 🤣
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.
@CodingWithMitch I love the content you create please keep up the good stuff like this.
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.
wow!
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.
@Nico Lucian instablaster =)
@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.
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
Thank you Mitch,
I was just struggling from handling GlobalScope!
Best Regard from Japan
woow just amazing... never ever got this kind of explanation.. love from India
I was looking forward to become a founding member but didn’t see any discount.
Has the discount promo been removed ?
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
Yeah that is coming up
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?
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
Save a timestamp when the data was refreshed. Then after x days refresh it
Mitch what model of microphone you use?
Thumbnail of the month 🏆
Thanks mitch 👍
Hi, how are you? Could you please make me lesson on recyclerview about multiselect in JAVA
. Thanks beforehand bro
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
}
}
can I use the global scope with delay for this method recyclerView.scrollToPosition(position)?
You can do whatever you want
good job
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.
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)
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
just add an invokeOnCompletion on the parent job. It will always fire when the job is complete or canceled
@@codingwithmitch I'm launching a suspendCoroutine inside the doWork. It doesn't seem to have an invokeOnCompletion method
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
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.
Damn I learnt this today. 🙏. Also be very careful with Fonts.
My fonts are best fonts
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
Nah just adding to a youtube playlist
@@codingwithmitch Great , can i get the link to the playlist :)
@@aabhishek4911 Look on my channel
@@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 ?
hi,
laucn{
.....
}
laucn{
.....
}
Does this code snipped when running in courotine, is it working sequantially or paralel?
what
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.
Always working on the website. Finding the time is difficult.
@@codingwithmitch Focus on that. That's where you get money :D
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.
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.
Neat explanation! GJ
GJ= Good Job not GlobalScope Job :P
Engagement
LIKE!
Sorry mitch. That is not an issue with global scope. Issue is structured concurrency.
now im always careful of globalscope
Hey mithc dont you get bored of doing Android Development daily. Are you exploring new technologies.
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.
That's the answer at the question everybody wants to know...Lebron, what's your decision?
ua-cam.com/video/RTeCc8jy7FI/v-deo.html