https://kotlinlang.org logo
Title
j

joadar

11/30/2021, 9:01 PM
Hello, is it possible to call a collect in a collect? I have this code:
dateFlow.collectLatest { date ->
    repository.getContentFromDate(date).collect { result ->
I can change the date so the content update. But I have a favorite feature on the content. On the default date, the favorite is working well (I collect the new value) but when I update the date, the collect doesn’t work on favorite events. What am I doing wrong? Thanks.
e

ephemient

11/30/2021, 9:05 PM
you probably want to do
dateFlow
    .flatMapLatest { date ->
        repository.getContentFromDate(date)
    }
    .collect { result ->
which will cancel the previous
repository.getContentFromDate
collection on each new
date
j

joadar

11/30/2021, 9:11 PM
Thanks, but still the same issue. I update dateFlow this way:
private val dateFlow: MutableStateFlow<Date> = MutableStateFlow(currentDate)
...
dateFlow.update {
   date
}
Maybe is it wrong?
The getContentFromDate:
override fun getContentFromDate(date: Date): Flow<Result<List<Content>>> = flow {
     emit(Result.Loading)
     ...
}
Aaaaaaah, my bad I managed the data in a specific way and I forgot to branch the data from Room during my network call (I saved the network content in room but I was returning the network “static” content and not the database content)
Thank you @ephemient for your help. At least the method you shared make it cleaner.
👍 1