Home > database >  How to make an emit from another function in a flow after flow.stateIn()?
How to make an emit from another function in a flow after flow.stateIn()?

Time:06-02

I get page data from a database, I have a repository that returns a flow.

class RepositoryImpl (private val db: AppDatabase) : Repository {

    override fun fetchData (page: Int) = flow {
        emit(db.getData(page))
    }
}

In the ViewModel, I call the stateIn(), the first page arrives, but then how to request the second page? By calling fetchData(page = 2) I get a new flow, and I need the data to arrive on the old flow.

class ViewModel(private val repository: Repository) : ViewModel() {

    val dataFlow = repository.fetchData(page = 1).stateIn(viewModelScope, WhileSubscribed())
}

How to get the second page in dataFlow?

CodePudding user response:

I don't see the reason to use a flow in the repository if you are emitting only one value. I would change it to a suspend function, and in the ViewModel I would update a variable of type MutableStateFlow with the new value. The sample code could look like the following:

class RepositoryImpl (private val db: AppDatabase) : Repository {

    override suspend fun fetchData (page: Int): List<Data> {
        return db.getData(page)
    }
}

class ViewModel(private val repository: Repository) : ViewModel() {

    val _dataFlow = MutableStateFlow<List<Data>>(emptyList())
    val dataFlow = _dataFlow.asStateFlow()

    fun fetchData (page: Int): List<Data> {
        viewModelScope.launch {
            _dataFlow.value = repository.fetchData(page) 
        }
    }    
}
  • Related