0

I have filters which I change on other fragment when I navigate back it saves these filters. Now, the issue is shared flow from time to time is not triggered and not proper data is displayed.

in ViewModel

    lateinit var archivedSettingsFlow: SharedFlow<ArchivedSettings>
[...]
  private fun createArchivedSettingsFlow() {
        val typesFlow = filtersRepository.getActiveFilters(FiltersType.ArchivedWorkItems.type)
        val sortByFlow = filtersRepository.getActiveFilter(FiltersType.ArchivedWorkItems.sortBy)
        val directionFlow =
            filtersRepository.getActiveFilter(FiltersType.ArchivedWorkItems.direction)
        val viewSettingsFlow = settingsRepository.getSettings(SettingsType.ArchivedWorkItems)

        val defaults =
            FiltersType.ArchivedWorkItems.defaultFilters().associateBy { filter -> filter.key }
        archivedSettingsFlow = combine(
            typesFlow,
            sortByFlow,
            directionFlow,
            viewSettingsFlow
        ) { types, sortBy, direction, viewSettings ->
            if (params.archivedMode) {
                _paramsFlow.update { params -> params.copy(currentPage = 0, hasNextPage = true) }
            }

            ArchivedSettings(
                types,
                sortBy ?: defaults.getValue(FiltersType.ArchivedWorkItems.sortBy.key),
                direction ?: defaults.getValue(FiltersType.ArchivedWorkItems.direction.key),
                viewSettings
            )
        }.debounce(250L)
            .shareIn(viewModelScope, SharingStarted.Lazily, replay = 1)
    }

in fragment in onViewCreated

        lifecycleScope.launchWhenResumed {
            viewModel.archivedSettingsFlow.filter { it != null }.collectLatest {
                onScrollListener?.reset()
                adapter.submitList(emptyList())
                viewModel.fetch()
            }
        }

I quite do not understand why it happens, i.e. it could work for 3-4 rounds and then it displays previous data depends on previous filters).

EDIT1: Timing in LogCat

2022-11-21 12:54:07.790 -> EMIT VALUE FIRST, PREVIOUS VALUES
2022-11-21 12:54:08.725 -> EMIT SECOND PROPER VALUES

after few times it only trigger first option "EMIT VALUE FIRST, PREVIOUS VALUES"
chrisChris
  • 89
  • 7
  • What do you think the debounce call is doing there? (The entire point of debounce is to drop data under certain conditions.) – Louis Wasserman Nov 21 '22 at 16:48
  • Immediately collects what is from upstream, but wait this (250L) to emit latest value. Without debounce it always collect first value (without filter change, previous values). Debounce here while emitting should drop first value and emit the latest(second). – chrisChris Nov 22 '22 at 08:14
  • Do you have any idea @LouisWasserman what should be changed here? – chrisChris Nov 22 '22 at 11:36

0 Answers0