Jrichards1408
11/10/2020, 12:41 PMobserveAsState()
to automatically populate a composable list view.
My composable looks like this
@Composable
fun getTopMovies() {
val topMovies by movieListViewModel.getTopMovies().observeAsState()
when (topMovies?.status) {
Status.Error -> Text("error")
Status.Loading -> {
Log.d("JJJ", "Loading ")
Text(text = "Loading")
}
Status.Success -> createMovieItemView(topMovies?.data?.results.orEmpty())
}
}
This uses MVVM to do a network call to fetch some list of data and return it it back as a livedata.
The issue i am having is that it seems stuck on a infinit loop. If i dont use observeeAsState
and just use the normal none composable way ie movieListViewModel.getTopMovies().observe(viewLifecycleOwner, Observer {
it works as expected and executes and ends once a error or a success is returned from the repository/domain layer.
This is the createMovieItemView
below:
@Composable
private fun createMovieItemView(movieList: List<MovieItem>) {
LazyColumnFor(items = movieList, itemContent = { movieItem ->
MovieListItem(MovieItemData(movieItem.posterPath.orEmpty(),
movieItem.title.orEmpty(),
movieItem.releaseDate.orEmpty(),
"some genra", ""), picasso)
})
}