I have 2 fragments, FragmentA contains a list of starwar characters whereas FragmentB contains details of that character. I am using viewModelScope.launch in my fragments to fetch details for a character. Below is my ViewModel
@HiltViewModel
class DetailsViewModel @Inject constructor(
private val getSpecieDetailsUseCase: GetSpecieDetailsUseCase,
private val getFilmDetailsUseCase: GetFilmDetailsUseCase,
private val getPlanetDetailsUseCase: GetPlanetDetailsUseCase,
private val mapper: CharacterDetailMapper
) : ViewModel() {
var characterDetailsModel = MutableLiveData<CharacterDetailsModel?>()
fun init(infoModel: CharacterInfoModel?) {
characterDetailsModel.postValue(null)
try {
viewModelScope.launch {
infoModel?.let {
val specieResponse = async(context = Dispatchers.IO) {
it.specieIdList?.map {
getSpecieDetailsUseCase.executeUseCase(
GetSpecieDetailsUseCase.GetSpecieDetailsRequest(
it
)
)
}
}
val filmResponse = async(context = Dispatchers.IO) {
it.filmsIdList?.map {
getFilmDetailsUseCase.executeUseCase(
GetFilmDetailsUseCase.GetFilmDetailsRequest(
it
)
)
}
}
val planetResponse = it.homeworldId?.let {
getPlanetDetailsUseCase.executeUseCase(
GetPlanetDetailsUseCase.GetPlanetDetailsRequest(
it
)
)
}
characterDetailsModel.postValue(
mapper.toModel(
name = it.name.toString(),
birth_year = it.birth_year.toString(),
height = it.height.toString(),
specieDetailsResponse = specieResponse.await(),
filmDetailsResponse = filmResponse.await(),
planetDetailsResponse = planetResponse
)
)
}
}
} catch (exception: Exception) {
exception.stackTrace
}
}
}
Above viewModelScope.launch
works perfectly fine when hit for the first time but does not work for the second time [i.e going back to the previous fragment and coming back on details fragment]. The data received in the init
function is also updated data however none of my async calls seem to work for the second time. onCleared
method of View Model is called every time when I go to the previous fragment which I feel should clear the scope. I tried catching an exception however there is no exception thrown. I tried debugging but none of my debug points for async calls are hit.