Combine with sharedFlow skip values

409 Views Asked by At

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"
0

There are 0 best solutions below