This is both Android and coroutines specific but t...
# coroutines
r
This is both Android and coroutines specific but think it may apply to more than just Android: I'm trying to come up with a way to have our own "global" scope that can also be replaced with a test coroutine scope for unit tests. Anyone have a recommendation or have sort of implementation like this now? My initial thought is to make the scope injectable via DI (dagger in my case). But not sure if that's overkill vs having some kind of static global scope. Thoughts?
z
having some kind of static global scope
Never do this. You make testing harder. Inject your scopes everywhere. Solves all kinds of issues
When our sdk/app is constructed, we create a global
CoroutineScope(dispatchers.default)
. And we also inject our dispatchers too. We disallow any reference to
Dispatchers.XXX
outside of this file, as well as
GlobalScope
s
Create a scope in app class, expose it with interface and inject that interface or provide scope from that application in constructor.
👍 1
r
@zak.taccardi how do you prevent outside scope usage? Is it just honor rule or maybe lint?
s
z
what do you mean by outside scope usage? Like someone using
GlobalScope
? We honor system it because we’re small enough, but a lint warning would suffice if we were a larger team
r
@zak.taccardi Yup I meant like GlobalScope. Thanks.
so what about if you have multi modules. Should you inject different global scope for each module or share one? thoughts @zak.taccardi @Sinan Kozak?
z
share the same global one across
but, you could decide to break structured concurrency too here
s
We have a ApplicationScope interface in base module and implement that in main application. We pass application scope interface. But i must say it is only for edge cases.