I’m working on an Android app and I launch a coroutine using GlobalScope at the start of my application, that coroutine then uses a flow to emit a value every 30 seconds for the lifetime of my application. I know that GlobalScope is generally not recommended, what should I be using instead?
n
Nick Allen
11/15/2021, 2:42 AM
Sounds like you are using it correctly.
👍 1
Nick Allen
11/15/2021, 2:46 AM
In a custom Application class or a singleton, that’s usually what you want. The warning is there because a lot of people will use it in other situations that should have shorter lifetimes than the process.
z
zsperske
11/15/2021, 3:32 AM
One thing I have noticed is that I’m seeing non fatal job cancellation exceptions in fabric which I think are when the user closes the app. Are coroutines launched with global scope meant to handle the cancellation exception?
n
Nick Allen
11/15/2021, 3:45 AM
I’m guessing you have some “catch everything and log to fabric” block. If you have exception handling code, you generally want to ignore those cancellation exceptions and just rethrow them.
l
Lefko
11/15/2021, 5:37 AM
Alternatively you can use the approach outlined here