JetPack撰写 - 记住CoroutinesCope,但带有钥匙

发布于 2025-02-07 03:11:31 字数 918 浏览 1 评论 0原文

如何获得绑定到可组合和某些钥匙值的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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

老街孤人 2025-02-14 03:11:31

我遇到了同样的问题,目前我开发了自己的解决方案。让我知道您对此的看法。默认情况下不可能感到很奇怪。

@Composable
inline fun rememberCoroutineScope(
    key: Any,
    crossinline getContext: @DisallowComposableCalls () -> CoroutineContext = { Dispatchers.Main }
) =
    remember(key) { CoroutineWrapper(getContext()) }

class CoroutineWrapper(
    coroutineContext: CoroutineContext
) : RememberObserver, CoroutineScope {
    override val coroutineContext = coroutineContext + Job()
    override fun onAbandoned() {
        cancel("Left composition")
    }
    override fun onForgotten() {
        cancel("Left composition")
    }
    override fun onRemembered() { }

}

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.

@Composable
inline fun rememberCoroutineScope(
    key: Any,
    crossinline getContext: @DisallowComposableCalls () -> CoroutineContext = { Dispatchers.Main }
) =
    remember(key) { CoroutineWrapper(getContext()) }

class CoroutineWrapper(
    coroutineContext: CoroutineContext
) : RememberObserver, CoroutineScope {
    override val coroutineContext = coroutineContext + Job()
    override fun onAbandoned() {
        cancel("Left composition")
    }
    override fun onForgotten() {
        cancel("Left composition")
    }
    override fun onRemembered() { }

}
掀纱窥君容 2025-02-14 03:11:31

尝试使用

@Composable
fun Sth(val sth: Int) {
    // `LaunchedEffect` will cancel and re-launch if `sth` changes
    LaunchedEffect(sth) {
        // call suspend functions
    }
}

启动效果输入构图时,它会启动一个coroutine
代码块作为参数传递。 Coroutine将是
如果启动效果取消了构图。如果
启动效应已重新组合不同的密钥,现有的
Coroutine将被取消,新的悬挂功能将是
在新的Coroutine中发射。


或尝试用启动启动Coroutine:

val coroutineScope = rememberCoroutineScope()

LaunchedEffect(key1 = sth) {
    // will be canceled and re-launched if sth is changed
    coroutineScope.launch() {
        // call suspend functions
    }
}

Try to use LaunchedEffect:

@Composable
fun Sth(val sth: Int) {
    // `LaunchedEffect` will cancel and re-launch if `sth` changes
    LaunchedEffect(sth) {
        // call suspend functions
    }
}

When LaunchedEffect enters the Composition, it launches a coroutine
with the block of code passed as a parameter. The coroutine will be
cancelled if LaunchedEffect leaves the composition. If
LaunchedEffect is recomposed with different keys, the existing
coroutine will be cancelled and the new suspend function will be
launched in a new coroutine.


Or try to wrap launching a coroutine with a LaunchedEffect:

val coroutineScope = rememberCoroutineScope()

LaunchedEffect(key1 = sth) {
    // will be canceled and re-launched if sth is changed
    coroutineScope.launch() {
        // call suspend functions
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文