https://kotlinlang.org logo
#coroutines
Title
# coroutines
r

Rechee Jozil

05/29/2020, 8:50 PM
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

zak.taccardi

05/29/2020, 8:51 PM
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

Sinan Kozak

05/29/2020, 8:56 PM
Create a scope in app class, expose it with interface and inject that interface or provide scope from that application in constructor.
👍 1
r

Rechee Jozil

05/29/2020, 8:57 PM
@zak.taccardi how do you prevent outside scope usage? Is it just honor rule or maybe lint?
s
z

zak.taccardi

05/29/2020, 8:57 PM
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

Rechee Jozil

05/29/2020, 8:58 PM
@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

zak.taccardi

05/29/2020, 9:13 PM
share the same global one across
but, you could decide to break structured concurrency too here
s

Sinan Kozak

05/30/2020, 7:08 AM
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.
4 Views