Hi everyone, working on an app with MVVM model whe...
# android
а
Hi everyone, working on an app with MVVM model where I have a fragment with list of news articles, view model and repository that gets data from api and stores to Room. I am using coroutines Flow to manage data from datasource to fragments in the following way (it’s simplified, to make the scope of exception smaller): In Fragment:
Copy code
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
    viewModel.news.collect {
    }
}
In ViewModel:
Copy code
private val newsFlow = refreshTrigger.flatMapLatest {
    repository.getNewsArticles()
}
val news = newsFlow.stateIn(viewModelScope, SharingStarted.Lazily, null)
In Repository:
Copy code
fun getNewsArticles() = channelFlow<Resource<List<NewsArticle>>> {
    try {
        newsArticleDao.getNews().collect { send(Resource.Success(listOf(NewsArticle("asfas", "asfas", "asfas")))) }
    }
    catch (e: Throwable) {
        Log.e("TEST", "error", e)
    }
}
I send value to refresh trigger when a user does swipe refresh. And each time they do it, I get the following exception in the Repository code:
Copy code
E/TEST: error
    kotlinx.coroutines.flow.internal.ChildCancelledException: Child of the scoped flow was cancelled
Can anyone explain whether it’s right behaviour or am I doing something wrong? Didn’t find a solution how to fix it. This is the most relevant post I found.