简体   繁体   English

从 Jetpack Compose 中获取 RoomDB 的值到 Composable function

[英]Get value from RoomDB into Composable function from Jetpack Compose

I have looked for many solutions but I found it as a newbie very complex on how to solve it properly without throwing away all my backend code.我已经寻找了许多解决方案,但我发现它作为一个新手非常复杂,如何在不丢弃我所有后端代码的情况下正确解决它。

I want to get an Float value from my RoomDB into a composable UI value but as far as we all know getting RoomDB values with queries needs an asynchronus scope. And those aren't capable of returning values because values stay within a scope and die there too.我想从我的 RoomDB 中获取一个 Float 值到一个可组合的 UI 值中,但据我们所知,通过查询获取 RoomDB 值需要一个异步 scope。而且那些不能返回值,因为值保持在 scope 内并死在那里也。 There is I think no way to no use Coroutine Scopes or anything else that doesn't block the UI loading so it can actually work.我认为没有办法不使用 Coroutine Scopes 或任何其他不会阻止 UI 加载的东西,这样它才能真正工作。

What can I do?我能做些什么? I don't want to throw away the entire RoomDB database neither our Jetpack Compose base GUI?我不想扔掉整个 RoomDB 数据库,也不想扔掉我们的 Jetpack Compose 基本 GUI?

I tried replacing the 0.8f with a method that calls a Coroutine Scope which should idealistically return a Float value to this part of our code.我尝试用调用协程 Scope 的方法替换 0.8f ,理想情况下,这应该向我们代码的这一部分返回一个 Float 值。

@Composable
fun ChargeScreen(){
    val context = LocalContext.current
    Box(
        contentAlignment = Alignment.Center,
        modifier = Modifier.fillMaxSize()
    ){
        Column {
            ChargeTopText()
            CircularChargeBar(percentage = 0.8f, number =100 )
        }
    }
}

I am also new at Android Jetpack Compose but I can give you a suggestion for your case.我也是 Android Jetpack Compose 的新手,但我可以为您的案例提供建议。

Take a look at the code below看看下面的代码

@Composable
fun YourComposable() {
    //Use remember for state management
    //Read more at https://developer.android.com/jetpack/compose/state
    var floatData by remember { mutableStateOf(0F) }
    var isLoading by remember { mutableStateOf(true) }

    //Side-effects
    //Read more at https://developer.android.com/jetpack/compose/side-effects
    LaunchedEffect(Unit) {
        isLoading = true
        floatData = getFloatDataFromDB()
        isLoading = false
    }

    //Just a way to show a progress indicator while we are getting the value from DB.
    if(!isLoading) {
        Text(text = "FloatData: $floatData")
    } else {
        CircularProgressIndicator(
            modifier = Modifier.size(50.dp),
            color = Color.Green,
            strokeWidth = 5.dp)
    }
}

suspend fun getFloatDataFromDB(): Float {
    //Using withContext(Dispatchers.IO) so it will execute in IO Thread.
    return withContext(Dispatchers.IO) {
        //Pretend this will take 5 seconds to complete
        Thread.sleep(5000)
        //And return the value
        return@withContext 0.9F
    }
}

I hope this will help you out!我希望这会帮助你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM