1

How to fix IllegalStateException: Attempt to collect twice from pageEventFlow, which is an illegal operation.

Did you forget to call Flow<PagingData<*>>.cachedIn(coroutineScope)?

Code:

import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.switchMap
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import com.kharismarizqii.movieapp.data.MovieRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import javax.inject.Inject

@HiltViewModel
class MovieViewModel @Inject constructor(
    private val repository: MovieRepository,
    state: SavedStateHandle) : ViewModel(){

    companion object{
        private const val CURRENT_QUERY = "current_query"
        private const val EMPTY_QUERY = ""
    }

    private val currentQuery = state.getLiveData(CURRENT_QUERY, EMPTY_QUERY)
    val movies = currentQuery.switchMap { query ->
        if (query.isNotEmpty()){
            repository.getSearchMovies(query)
        }else{
            repository.getNowPlayingMovies().cachedIn(viewModelScope)
        }
    }

    fun searchMovies(query: String){
        currentQuery.value = query
    }
}

Crash:

  java.lang.IllegalStateException: Attempt to collect twice from pageEventFlow, which is an illegal operation. Did you forget to call Flow<PagingData<*>>.cachedIn(coroutineScope)?
  

enter image description here

Bolt UIX
  • 5,988
  • 6
  • 31
  • 58

1 Answers1

8

You should cache the data for getSearchMovies too

    private val currentQuery = state.getLiveData(CURRENT_QUERY, EMPTY_QUERY)
    val movies = currentQuery.switchMap { query ->
        if (query.isNotEmpty()){
            repository.getSearchMovies(query).cachedIn(viewModelScope)
        }else{
            repository.getNowPlayingMovies().cachedIn(viewModelScope)
        }
    }
Fayaz
  • 1,846
  • 2
  • 16
  • 21