androidandroid-jetpack-composeandroid-lifecyclemutablelivedata

How to Updating jetpack compose with MutableLiveData.observeAsState() more often


fun Conversation(mainViewModel: MainViewModel) {
    var stat = mainViewModel.getTweet().observeAsState()
    val listState = rememberScrollState()
    stat?.let {
        if( stat.value == null){
            Log.d("DAD","DASSA")
            // null 값이면 로딩 에니메이션 뜨게 만들면 좋을듯>????
        }
        else{
            Log.d("DAD","됐다")
        var author = it.value!!.first
        var messages = it.value!!.second
        LazyColumn(modifier = Modifier.verticalScroll(listState).height(100.dp)) {
            items(messages) { message ->
                MessageCard(author, message)
            }
        }
        }
    }
}

In this composable, it seems like the composable "DID NOT" update value of mutableLivedata( mainViewModel.getTweet()). but I confuse about below code

fun InfoCard(mainViewModel: MainViewModel){
    val Name = mainViewModel.getData().observeAsState()


    Name.value?.let { it ->

        Row(modifier = Modifier
            .fillMaxWidth()
            .background(shape = RoundedCornerShape(6.dp), color = Color.LightGray)
            .padding(30.dp)
            .height(40.dp),
            verticalAlignment = Alignment.CenterVertically

        ) {
            Spacer(modifier = Modifier.height(15.dp))
            Text(it.get(0), modifier = Modifier.width(200.dp), textAlign = TextAlign.Center)
            Spacer(modifier = Modifier.width(30.dp))
            Text(text = it.get(1))
        }
    }
}

it also composable and use value of mutableLivedata( mainViewModel.getData()) But IT UPDATE VERY WELL.. what is a difference and How can I update first composable..


Solution

  • Self solution for someone like me.
    Change

    val Name = mainViewModel.getData().observeAsState()
    

    to

    val Name by mainViewModel.getData().observeAsState()