kenkyee
02/01/2024, 8:57 PMval appScope =
object : CoroutineScope {
override val coroutineContext: CoroutineContext
get() = EmptyCoroutineContext + CoroutineName("AppScope")
}
kevin.cianfarini
02/01/2024, 8:59 PMkevin.cianfarini
02/01/2024, 9:00 PMkenkyee
02/01/2024, 9:00 PMkenkyee
02/01/2024, 9:01 PMkevin.cianfarini
02/01/2024, 9:03 PMrunBlocking
. At boundaries like this you’re supposed to exercise more caution. For example, you aren’t supposed to call runBlocking from coroutines code because it can potentially result in deadlock.
The compiler won’t/can’t enforce this, though. Likewise here, you’re entering into coroutines world, so it’s a bit strange to try and pass it some information that already exists in coroutine world (namely, CoroutineScope).kevin.cianfarini
02/01/2024, 9:03 PMrxSingle
coroutine a name, and truly don’t need to scope, just pass in a name as the coroutine context?kenkyee
02/01/2024, 9:04 PMkevin.cianfarini
02/01/2024, 9:05 PMrxSingle(CoroutineName("Ken Yee")) { ... }
Work?kenkyee
02/01/2024, 9:07 PMkevin.cianfarini
02/01/2024, 9:19 PMCoroutineScope.rxSingle
in favor of a top level one that runs in Globalscope.
https://github.com/Kotlin/kotlinx.coroutines/commit/d100a3f753fdfcdbdae5d82b8ef9f01126ad6a7ckenkyee
02/02/2024, 2:52 PMkevin.cianfarini
02/02/2024, 4:02 PMrxSingle
in a scope other than globalscope? The latter is impossible for good reasons, the former is possible by just passing in a coroutine name.kenkyee
02/02/2024, 4:33 PMDmitry Khalanskiy [JB]
02/05/2024, 10:10 AMrxSingle(CoroutineName("something"))
That doesn't work because a CoroutineName is not a CoroutineScopeNo, it does work, I just checked.
rxSingle
doesn't need a CoroutineScope
, it needs a CoroutineContext
.Dmitry Khalanskiy [JB]
02/05/2024, 10:19 AMmore to discourage folks from using Globalscope (and trigger the detekt lint warning).How does
rxSingle
help with that? Do you mean that you just want to replace GlobalScope.launch
with rxSingle
? This doesn't solve the fundamental problems of GlobalScope
. Its problem is not a "lint warning", but why the warning is there in the first place: because it's not part of structured concurrency. I suggest not to alias GlobalScope
but to try to restructure the code so that it doesn't need rxSingle
or GlobalScope
. rxSingle
is strictly for interacting with code that depends on RxJava.kenkyee
02/05/2024, 11:27 AMDmitry Khalanskiy [JB]
02/05/2024, 11:55 AMrxSingle(CoroutineName("..."))
is the way to go.kenkyee
02/05/2024, 1:41 PMfun something(): Single<SomeData> = rxSingle {
that uses the current coroutine scope (which happens to be globalscope) and that's where rxSingle flagged the bad scope issue when I tried to replace all the GlobalScope.launch calls with AppScope.
This is so that something() can be used by older Rx code but we want all new code to use coroutines.
AppScopes would normally be things like "network is still available" (cancelled when it's lost), etc. besides the full lifetime AppScope.Dmitry Khalanskiy [JB]
02/05/2024, 1:46 PMsomething()
is just fine if it's written for consumption by code that uses RxJava, where's the problem?kenkyee
02/05/2024, 1:51 PMDmitry Khalanskiy [JB]
02/05/2024, 1:52 PMGlobalScope.
there? You can just do rxSingle { launch { stuff } }
.kenkyee
02/05/2024, 1:54 PMDmitry Khalanskiy [JB]
02/05/2024, 1:56 PMkenkyee
02/05/2024, 1:58 PM): Single<ColorThievingResult> = rxSingle(AppScopes.getGlobal(context).coroutineContext) {
buildBillboardGradients(image, imageUrl, rectToSample, rtl)
}
In that case, the AppScopes wasn't even needed 😞
Will confirm and remove...thanks for the reminder.