0

I'm migrating an app from views to compose and am trying to trigger a schedule update every five minutes in which if there is a change in the data compose should recompose. Still using RxJava in my vm, but I see getDataList() get called every 5 minutes, but I'm unsure how to update the list when dealing with state.

Here's my code so far: In compose

 val dataList by viewModel.data.observeAsState() LaunchedEffect(Unit) { while(true) { viewModel.getDataList() delay(5 * 60 * 1000) } } 

In viewmodel

private val _data = MutableLiveData<List<Item>>() val data: LiveData<List<Item>> = _data fun getDataList() { compositeDisposable.add(repository .getData() .subscribe( { data -> _data.postValue(data) }, { it.printStackTrace() })) } 
1

1 Answer 1

1

LazyColumn and derivatives are pretty straightforward:

 val dataList by viewModel.data.observeAsState(emptyList()) LazyColumn { items(dataList, key = { item -> item.id }) { data -> Text("Something $data") } } 

key is optional, but it's a good optimization to reduce number of recompositions. Inside the itemContent @Composable closure you define how each item should be rendered - in my case a simple Text

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I'm looking for a way to trigger the data to be fetched and my composables to be recomposed every 5 minutes.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.