In my ViewModel, I am making API requests and I am using StateFlow and SharedFlow to communicate with the Fragment. While making the API request, I am easily able to update the state flow's value and it is successfully collected in the Fragment.
But before making the request, I am emitting some boolean values with SharedFlow and it is not getting collected in the Fragment. Can someone help me why is this happening?
class MainViewModel: ViewModel() {
  private val _stateFlow = MutableStateFlow(emptyList<Model>())
  val stateFlow = _stateFlow.asStateFlow()
  private val _loading = MutableSharedFlow<Boolean>()
  val loading = _loading.asSharedFlow()
  suspend fun request() {
    _loading.emit(true)
    withContext(Dispatchers.IO) {
      /* makes API request */
      /* updates _stateFlow.value */
      /* stateFlow value is successfully collected */
    }
    _loading.emit(false) // emitting boolean value
  }
}
class MyFragment : Fragment(R.layout.fragment_my) {
   // ...
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    lifecycleScope.launchWhenStarted {
      viewModel.request()
      /* stateFlow is collected and triggered - working properly */
      viewModel.loading.collectLatest { // <- NOT COLLECTING - WHY?
        Log.d(this::class.simpleName, "onViewCreated: $it") // <- NOT LOGGING
      }
    }
  }
}
 
                        
I guess you need to launch a different coroutine to collect loading values, something like the following:
viewModel.request()function is asuspendfunction, it suspends the coroutine until it is finished. But I guess it is not finishing due to callingsuspendfunction_loading.emit(), suspending until it is collected.Or I think it is even better would be to launch a coroutine in
ViewModelclass, something like the following: