calidion
01/22/2025, 8:26 AMfun PeerMain(updatePage: ((value: PageType) -> Unit)) {
    Column(
        modifier = Modifier.padding(horizontal = 2.dp),
        verticalArrangement = Arrangement.Center,
        horizontalAlignment = Alignment.CenterHorizontally
    ) {
        var scope = MainApplication.instance!!.coroutineScope;
        var accounts: List<Account>
        scope.launch {
            accounts = MainApplication.instance!!.db.account().getAll()
            if (accounts.isNotEmpty()) {
                accounts.forEach {
                    Text(it.address)
                }
            } else {
                Text(
                    "PeerMain"
                )
            }
        }
    }
}
how to correct this code?Chrimaeon
01/22/2025, 8:57 AMaccounts need to be a state to have the text field updated it state. You can use produceState to call your suspend function.
https://developer.android.com/develop/ui/compose/side-effects#producestateChrimaeon
01/22/2025, 8:59 AMcalidion
01/22/2025, 10:09 AMcalidion
01/22/2025, 11:37 AMval accountList = remember { mutableStateOf<List<Account>>(ArrayList()) }
    LaunchedEffect(Unit) {
        val scope = MainApplication.instance!!.coroutineScope
        scope.launch {
            accountList.value = MainApplication.instance!!.db.account().getAll()
        }
    }
    // If the list is empty, it means that our coroutine has not completed yet and we just want
    // to show our loading component and nothing else. So we return early.
    if (accountList.value.isEmpty()) {
        Text("No Peer Found!")
        return
    } else {
        accountList.value.forEach {
            Text(it.address)
        }
    }Chrimaeon
01/22/2025, 11:42 AMproduceState your code will be more readable.
Also you do not need a scope , the LaunchedEffect is started in a coroutine scope.calidion
01/22/2025, 11:43 AMcalidion
01/22/2025, 11:44 AMcalidion
01/22/2025, 11:44 AMChrimaeon
01/22/2025, 11:46 AMval accountList = produceState(emptyList()) {
            value = MainApplication.instance!!.db.account().getAll()
        }calidion
01/22/2025, 11:50 AMjava.lang.IllegalStateException: Cannot access database on the main thread since it may potentially lock the UI for a long period of time.Chrimaeon
01/22/2025, 11:51 AMsuspend function, right?calidion
01/22/2025, 11:52 AMcalidion
01/22/2025, 11:52 AMval accountList = remember { mutableStateOf<List<Account>>(ArrayList()) }
    LaunchedEffect(Unit) {
        val scope = MainApplication.instance!!.coroutineScope
        scope.launch {
            accountList.value = MainApplication.instance!!.db.account().getAll()
        }
    }
this snippet works.Chrimaeon
01/22/2025, 11:55 AMwithContext to switch away from the main thread:
val accountList =
            produceState(emptyList<String>()) {
                value =
                    withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
                        MainApplication.instance!!
                            .db
                            .account()
                            .getAll()
                    }
            }calidion
01/22/2025, 11:58 AMChrimaeon
01/22/2025, 11:59 AMChrimaeon
01/22/2025, 12:00 PMcalidion
01/22/2025, 12:01 PM