JetPack撰写 - 记住CoroutinesCope,但带有钥匙
如何获得绑定到可组合和某些钥匙值的Coroutine范围?基本上,我想获得类似的东西:
@Composable
fun Sth(val sth: Int) {
val coroutineScope = rememberCoroutineScope(sth)
}
我需要在呼叫留下构图时取消范围(就像remage coroutinesscope
),而且还需要键sth
更改时。
更新:
我需要此功能的一个地方:
class SomeIndication(
val a: Int,
val b: Int
) : Indication {
@Composable
override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance {
val coroutineScope = rememberCoroutineScope(interactionSource)
return remember(interactionSource) {
val sth: State<Int> = sth(a, b, coroutineScope)
object: IndicationInstance {
override fun ContentDrawScope.drawIndication() {
drawContent()
drawSomething(x.value)
}
}
}
}
}
How can I obtain a coroutine scope bound to a composable but also to some key values? Basically I want to obtain something like this:
@Composable
fun Sth(val sth: Int) {
val coroutineScope = rememberCoroutineScope(sth)
}
I need the scope to be canceled when the call leaves the composition (just like with rememberCoroutineScope
), but also when the key sth
changes.
Update:
One place in which I need this functionality:
class SomeIndication(
val a: Int,
val b: Int
) : Indication {
@Composable
override fun rememberUpdatedInstance(interactionSource: InteractionSource): IndicationInstance {
val coroutineScope = rememberCoroutineScope(interactionSource)
return remember(interactionSource) {
val sth: State<Int> = sth(a, b, coroutineScope)
object: IndicationInstance {
override fun ContentDrawScope.drawIndication() {
drawContent()
drawSomething(x.value)
}
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我遇到了同样的问题,目前我开发了自己的解决方案。让我知道您对此的看法。默认情况下不可能感到很奇怪。
I faced the same problem, I currently developed my own solution. Let me know what you think about it. Feels weird that it is not possible by default.
尝试使用
:
或尝试用
启动
启动Coroutine:Try to use
LaunchedEffect
:Or try to wrap launching a coroutine with a
LaunchedEffect
: