From 245241e6146c1e9bc4cccac2820f480f4d6c6085 Mon Sep 17 00:00:00 2001 From: Fanchao Liu <273191+simophin@users.noreply.github.com> Date: Wed, 21 Aug 2024 16:08:36 +1000 Subject: [PATCH] Fix search ANR (#1634) Co-authored-by: fanchao --- .../home/search/GlobalSearchViewModel.kt | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt b/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt index fb4a61a558..4a25556a42 100644 --- a/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt +++ b/app/src/main/java/org/thoughtcrime/securesms/home/search/GlobalSearchViewModel.kt @@ -10,21 +10,19 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow import kotlinx.coroutines.flow.MutableStateFlow -import kotlinx.coroutines.flow.SharingStarted import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOf import kotlinx.coroutines.flow.map import kotlinx.coroutines.flow.mapLatest import kotlinx.coroutines.flow.merge -import kotlinx.coroutines.flow.stateIn import kotlinx.coroutines.launch import kotlinx.coroutines.plus -import org.session.libsignal.utilities.SettableFuture import org.thoughtcrime.securesms.search.SearchRepository import org.thoughtcrime.securesms.search.model.SearchResult -import java.util.concurrent.TimeUnit import javax.inject.Inject +import kotlin.coroutines.resume +import kotlin.coroutines.suspendCoroutine @OptIn(ExperimentalCoroutinesApi::class) @HiltViewModel @@ -47,11 +45,8 @@ class GlobalSearchViewModel @Inject constructor( // User input delay in case we get a new query within a few hundred ms this // coroutine will be cancelled and the expensive query will not be run. delay(300) - val settableFuture = SettableFuture() - searchRepository.query(query.toString(), settableFuture::set) try { - // search repository doesn't play nicely with suspend functions (yet) - settableFuture.get(10_000, TimeUnit.MILLISECONDS).toGlobalSearchResult() + searchRepository.suspendQuery(query.toString()).toGlobalSearchResult() } catch (e: Exception) { GlobalSearchResult(query.toString()) } @@ -69,6 +64,12 @@ class GlobalSearchViewModel @Inject constructor( } } +private suspend fun SearchRepository.suspendQuery(query: String): SearchResult { + return suspendCoroutine { cont -> + query(query, cont::resume) + } +} + /** * Re-emit whenever refreshes emits. * */