By default, the way Google intended (and which DOES try to re-fetch in onStart) you would make StarWarsApiService.getFilms
be suspend fun
instead of callback-based, then you can do
class FilmsViewModel(private val savedStateHandle: SavedStateHandle): ViewModel() {
val filmsLiveData = liveData {
emit(loadFilms())
}
}
But what you're looking for is a regular viewModelScope.launch {
that saves the retrieved data in a MutableLiveData
, OR into local storage (Room) that would expose a LiveData<List<T>>
which would be observed by the Fragment.
SavedStateHandle isn't for data, it is for state.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…