Android Room可观察的查询不会使用ViewModel第二次触发

发布于 2025-02-07 21:55:14 字数 1574 浏览 2 评论 0原文

在过去的10个小时中,寻找解决我的问题的解决方案,但我无法理解问题到底是什么,这使我发疯。

我有一个viewModel其中init我正在收集flow像这样:

@HiltViewModel
class ExampleViewModel @Inject constructor(private val repository: Repository) : ViewModel() {
    init {
        viewModelScope.launch {
            taxCalendarUseCase
                .observeCalendarBetweenDates(
                    startDate = uiState.selectedMonth.value.atDay(1),
                    endDate = uiState.selectedMonth.value.atEndOfMonth()
                )
                .distinctUntilChanged()
                .collect { list ->
                    Timber.i("Collect is called with ${list.size} items")
                    uiState.taxCalendar.value = list
                    uiState.selectedCountry.value =
                        uiState.taxCalendar.value.map { it.countryCode }.distinct().firstOrNull()
                            ?: ""
                }
        }
    }
}

我的dao看起来像这样:

@Dao
interface TaxCalendarDao {

    @Query("SELECT * FROM tax_calendar WHERE date BETWEEN :start AND :end")
    fun observeTaxCalendarListBetweenDates(start: String, end: String): Flow<List<TaxCalendarModel>>
}

基于我在收集块中放置的日志,当我第一次打开composable时(这是屏幕a),然后转到另一个composable(屏幕b)我在数据库中添加一些数据的地方,当导航时,我期望再次调用collect,但这不会发生。

EDIT1:我尝试更改调度程序,以添加oneach,以删除distractillChanged以制作mutableStateFlow并在中收集存储库

Spent the last 10 hours searching for a solution to my problem, but I am not able to understand what exactly is the problem and it's driving me crazy.

I have a viewmodel where on init I am collecting a flow like this:

@HiltViewModel
class ExampleViewModel @Inject constructor(private val repository: Repository) : ViewModel() {
    init {
        viewModelScope.launch {
            taxCalendarUseCase
                .observeCalendarBetweenDates(
                    startDate = uiState.selectedMonth.value.atDay(1),
                    endDate = uiState.selectedMonth.value.atEndOfMonth()
                )
                .distinctUntilChanged()
                .collect { list ->
                    Timber.i("Collect is called with ${list.size} items")
                    uiState.taxCalendar.value = list
                    uiState.selectedCountry.value =
                        uiState.taxCalendar.value.map { it.countryCode }.distinct().firstOrNull()
                            ?: ""
                }
        }
    }
}

And my DAO looks like this:

@Dao
interface TaxCalendarDao {

    @Query("SELECT * FROM tax_calendar WHERE date BETWEEN :start AND :end")
    fun observeTaxCalendarListBetweenDates(start: String, end: String): Flow<List<TaxCalendarModel>>
}

Based on the log I've put in the collect block, when I open my Composable the first time it's called (This is screen A), then going to another Composable (Screen B) where I add some data to the database, when navigating up I expect collect being called again but this is not happening.

Edit1: I've tried to change the dispatcher, to add onEach, to remove distinctUntillChanged to make a mutableStateFlow and collecting in the repository

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文