gpt4 book ai didi

android - 在 Jetpack Compose 中使用 LaunchedEffect 与 SideEffect

转载 作者:行者123 更新时间:2023-12-05 09:01:13 28 4
gpt4 key购买 nike

大家好,我正在学习项目中的副作用。我想知道什么时候应该在什么场景下使用LaunchedEffectSideEffect。我正在使用这两种效果添加一些代码。如果我在这里做错了,请告诉我。

第一次使用LaunchedEffect,请指导我是否需要对此功能进行影响。

@Composable
fun BluetoothRequestContinue(multiplePermissionsState: MultiplePermissionsState) {
var launchPermission by remember { mutableStateOf(false) }
if (launchPermission) {
LaunchedEffect(Unit) {
multiplePermissionsState.launchMultiplePermissionRequest()
}
}
AbcMaterialButton(
text = stringResource(R.string.continue_text),
spacerHeight = 10.dp
) {
launchPermission = true
}
}

第二次使用 SideEffect 使用 intent 打开设置

@Composable
fun OpenPermissionSetting(router: Router = get()) {
val activity = LocalContext.current as Activity
var launchSetting by remember { mutableStateOf(false) }
if (launchSetting) {
SideEffect {
activity.startActivity(router.permission.getPermissionSettingsIntent(activity))
}
}
AbcMaterialButton(
text = stringResource(R.string.open_settings),
spacerHeight = 10.dp
) {
launchSetting = true

}
}

请告诉我是否需要 Effect。如果我们也需要不同的效果,也请指导我。谢谢

最佳答案

有区别

   if (launchSetting) {
SideEffect {
// Do something
}
}

   if (launchPermission) {
LaunchedEffect(Unit) {
multiplePermissionsState.launchMultiplePermissionRequest()
}
}

两者都在条件为真时进入重组,但 LaunchedEffect 只被调用一次,因为它的键是 Unit。只要条件为真,就会在每次重组时调用 SideEffect

SideEffect 函数可用于仅当重组成功时才应调用的操作

Recomposition starts whenever Compose thinks that the parameters of acomposable might have changed. Recomposition is optimistic, whichmeans Compose expects to finish recomposition before the parameterschange again. If a parameter does change before recompositionfinishes, Compose might cancel the recomposition and restart it withthe new parameter.

When recomposition is canceled, Compose discards the UI tree from therecomposition. If you have any side-effects that depend on the UIbeing displayed, the side-effect will be applied even if compositionis canceled. This can lead to inconsistent app state.

Ensure that all composable functions and lambdas are idempotent andside-effect free to handle optimistic recomposition.

Sample from official docs

To share Compose state with objects not managed by compose, use theSideEffect composable, as it's invoked on every successfulrecomposition.

@Composable
fun rememberAnalytics(user: User): FirebaseAnalytics {
val analytics: FirebaseAnalytics = remember {
/* ... */
}

// On every successful composition, update FirebaseAnalytics with
// the userType from the current User, ensuring that future analytics
// events have this metadata attached
SideEffect {
analytics.setUserProperty("userType", user.userType)
}
return analytics
}

LaunchedEffect 用于在组合或键更改时重新组合时调用函数。

如果您将 LaunchedEffect 写成

LaunchedEffect(key1= launchPermission) {
if(launchPermission) {
// Do something
}
}

if 中的代码块如果 key 不为真则不会在组合中调用,但只要它从 false 变为 true 代码块就会被调用。这对于不由用户交互直接触发的一次性操作或当操作需要在用户交互、动画或调用挂起函数(例如 lazyListState.animateScrollToItem()

副作用概念的定义来自Wikipedia

In computer science, an operation, function or expression is said tohave a side effect if it modifies some state variable value(s) outsideits local environment, which is to say if it has any observable effectother than its primary effect of returning a value to the invoker ofthe operation. Example side effects include modifying a non-localvariable, modifying a static local variable, modifying a mutableargument passed by reference, performing I/O or calling otherfunctions with side-effects. In the presence of side effects, aprogram's behaviour may depend on history; that is, the order ofevaluation matters. Understanding and debugging a function with sideeffects requires knowledge about the context and its possiblehistories.

关于android - 在 Jetpack Compose 中使用 LaunchedEffect 与 SideEffect,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73799916/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com