I am trying to get some data from a webservice that I only need to call once per start of my app, as the data is updated so seldom.
My view model is the core of this section.
val courseList = MutableLiveData<CourseCatalog>()
init {
viewModelScope.launch {
val courses = ecRepository.fetchCourses { it -> courseList.postValue(it) }
logger.i { "Number of courses returned is ${courseList.value?.courses?.size}" }
}
}
It calls my repository and this does work, and the ViewModel logger also prints the number of courses returned.
suspend fun fetchCourses(courseListNavigation: (courseList:CourseCatalog) -> Unit) {
logger.d { "fetchAndStoreCourses" }
courseList = CourseCatalog(ecApi.fetchCourseCatalog().toList())
courseListNavigation(courseList!!)
}
But here my view never gets to items=it1.courses
and so never gets to CourseView
val ecViewModel = getViewModel<ECViewModel>()
val courseListState = ecViewModel.courseList
Scaffold(
topBar = {
TopAppBar(title = { Text("People In Space") })
}) {
LazyColumn(contentPadding = paddingValues) {
courseListState.value?.let { it1 ->
items(
items = it1.courses
) { person ->
CourseView(person, personSelected)
}
}
}
}
Why am I not having the mutablelivedata.postvalue not sending a message to courseListState so the view can be recomposed?
I am using Kotlin 1.4.32 and jetpack compose 1.0.0-beta06.