ildar.i [Android]
04/04/2022, 12:16 PMprivate var currentSearch: Job? = null
private val debounceDelayMs = 500L
private val _searchPagingFlow = MutableSharedFlow<PagingData<TaskModel>>()
override val searchPagingFlow: Flow<PagingData<TaskModel>> = _searchPagingFlow
override fun searchByText(text: String) {
currentSearch?.cancel()
currentSearch = ioThenMain({
delay(debounceDelayMs)
repository.searchTasks(text)
}) { pagingFlow ->
pagingFlow.collect {
_searchPagingFlow.emit(it)
}
}
}
but this approach won’t save current search items, because I’m using SharedFlow. What can I do here to make it work properly?Joffrey
04/05/2022, 10:39 AMioThenMain
here? I'm not sure how you're handling your jobs here with ioThenMain
because I'm not aware of such function. Looks like it starts some coroutine outside of structured concurrency - which would be quite bad. Where is the scope for this job?Joffrey
04/05/2022, 10:42 AMildar.i [Android]
04/05/2022, 10:47 AMildar.i [Android]
04/05/2022, 11:39 AMrepository.searchTasks(it)
every time I come back to this screen. Can it be because of collectAsLazyPagingItems
?ildar.i [Android]
04/05/2022, 12:06 PMcachedIn(scope)
. Now it’s working perfectly. Thanks, man!Joffrey
04/05/2022, 1:03 PMioThenMain
function so it integrates better with structured concurrency so you avoid potential coroutine leaksildar.i [Android]
04/05/2022, 7:05 PMioThenMain
is called makeRequest
here
basically we create our own scope with SupervisorJob
and then launch our coroutines inside of it. The only problem might be with CancellationException
suppression inside makeRequest
, but it handled properly, imho