From dccb73c3ccadbdf880b46e67795e717a4ed460b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jorge=20Mart=C3=ADn?= Date: Fri, 10 Jan 2025 14:35:38 +0100 Subject: [PATCH] Run `SyncService.start` and `stop` with a non-UI dispatcher --- .../libraries/matrix/impl/RustMatrixClient.kt | 6 +++++- .../libraries/matrix/impl/sync/RustSyncService.kt | 12 +++++++++--- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt index 0b7ae4f4078..89ca27ccd1e 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/RustMatrixClient.kt @@ -137,7 +137,11 @@ class RustMatrixClient( private val innerRoomListService = innerSyncService.roomListService() - private val rustSyncService = RustSyncService(innerSyncService, sessionCoroutineScope) + private val rustSyncService = RustSyncService( + innerSyncService = innerSyncService, + sessionCoroutineScope = sessionCoroutineScope, + syncServiceDispatcher = sessionDispatcher + ) private val pushersService = RustPushersService( client = innerClient, dispatchers = dispatchers, diff --git a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt index 667f5712013..a16687f5e3a 100644 --- a/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt +++ b/libraries/matrix/impl/src/main/kotlin/io/element/android/libraries/matrix/impl/sync/RustSyncService.kt @@ -9,6 +9,7 @@ package io.element.android.libraries.matrix.impl.sync import io.element.android.libraries.matrix.api.sync.SyncService import io.element.android.libraries.matrix.api.sync.SyncState +import kotlinx.coroutines.CoroutineDispatcher import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.NonCancellable import kotlinx.coroutines.flow.SharingStarted @@ -25,7 +26,8 @@ import org.matrix.rustcomponents.sdk.SyncService as InnerSyncService class RustSyncService( private val innerSyncService: InnerSyncService, - sessionCoroutineScope: CoroutineScope + sessionCoroutineScope: CoroutineScope, + private val syncServiceDispatcher: CoroutineDispatcher, ) : SyncService { private val isServiceReady = AtomicBoolean(true) @@ -35,7 +37,9 @@ class RustSyncService( return@runCatching } Timber.i("Start sync") - innerSyncService.start() + withContext(syncServiceDispatcher) { + innerSyncService.start() + } }.onFailure { Timber.d("Start sync failed: $it") } @@ -46,7 +50,9 @@ class RustSyncService( return@runCatching } Timber.i("Stop sync") - innerSyncService.stop() + withContext(syncServiceDispatcher) { + innerSyncService.stop() + } }.onFailure { Timber.d("Stop sync failed: $it") }