When I change the isInFavorites property of the GameData in a different fragment I can see that the change is received in my repository's listener but when I navigate back to the fragment, my viewmodel never receives the updated value when I use the MutableStateFlow.
Strange enough, when I change my flow to the MutableSharedFlow, all of a sudden viewmodel starts to get the updated values as well. Does anyone have any idea why this might be happening? I need to use the MutableStateFlow here and it's not working.
Repository:
private val gameDataListResultMutableFlow: MutableStateFlow<Result<List<GameData>>> = MutableStateFlow(Result.Loading)
override suspend fun observeGameDataList(): Flow<Result<List<GameData>>>
{
CoroutineScope(Dispatchers.IO + coroutineContext).launch {
localGameDataSource.observeGameDataList().collectLatest{
if(it is Result.Success)
{
Timber.d("local data change favorite value of item 0: ${it.data[0].isInFavorites}")
}
gameDataListResultMutableFlow.emit(it)
}
}
}
Viewmodel:
private suspend fun observeGameListResult()
{
gameRepository.observeGameDataList().collect{
if(it is Result.Success)
Timber.d("data change received in viewmodel value of item 0: ${it.data[0].isInFavorites}")
gameListResultMutableLiveData.postValue(it)
}
}
fun getGameListResultLiveData(): LiveData<Result<List<GameData>>>
{
launch(coroutineContext) {
observeGameListResult()
}
return gameListResultMutableLiveData
}
Logs When StateFlow is Used
LOADING THE INITIAL STATE, ISFAVORITE VALUE IS TRUE
D/DefaultGameRepository: local data change favorite value of item 0: true
D/GameListViewModel: data change received in viewmodel value of item 0: true
SWITCHING TO ANOTHER FRAGMENT TO CHANGE THE ISFAVORITE'S VALUE TO FALSE, WHICH IS RECEIVED ONLY BY
THE LOCAL SOURCE LISTENER
D/DefaultGameRepository: local data change the favorite value of item 0: false
SWITCHING BACK TO THE INITIAL FRAGMENT AND THE UPDATED VALUE OF THE ISFAVORITE REFLECTED ON THE LOCAL SOURCE LISTENER BUT NOT ON THE VIEWMODEL LISTENER---
D/GameListViewModel: data change received in viewmodel value of item 0: true
D/DefaultGameRepository: local data change the favorite value of item 0: false
Logs When SharedFlow is Used:
LOADING THE INITIAL STATE, ISFAVORITE VALUE IS TRUE
D/DefaultGameRepository: local data change the favorite value of item 0: true
D/GameListViewModel: data change received in viewmodel value of item 0: true
SWITCHING TO ANOTHER FRAGMENT TO CHANGE THE ISFAVORITE'S VALUE TO FALSE, WHICH IS RECEIVED BY LOCAL
SOURCE AND THE VIEWMODEL LISTENER
D/DefaultGameRepository: local data change the favorite value of item 0: false
D/GameListViewModel: data change received in viewmodel value of item 0: false
SWITCHING BACK TO THE INITIAL FRAGMENT AND THE UPDATED VALUE OF THE ISFAVORITE REFLECTED ON THE
VIEWMODEL
D/DefaultGameRepository: local data change the favorite value of item 0: false
D/GameListViewModel: data change received in viewmodel value of item 0: false
D/GameListViewModel: data change received in viewmodel value of item 0: false