From 2ea6cdba6f779b2d474a3dd706c2be2b16fff21c Mon Sep 17 00:00:00 2001 From: valere Date: Tue, 23 May 2023 08:01:32 +0200 Subject: [PATCH] Fix app non responsive --- changelog.d/8454.bugfix | 1 + .../session/room/read/SetReadMarkersTask.kt | 5 ++- .../DeleteUnusedClientInformationUseCase.kt | 26 ++++++++------- .../home/room/list/RoomListViewModel.kt | 9 +++--- .../NotificationDrawerManager.kt | 32 ++++++++++++------- 5 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 changelog.d/8454.bugfix diff --git a/changelog.d/8454.bugfix b/changelog.d/8454.bugfix new file mode 100644 index 0000000000..01d30cacdd --- /dev/null +++ b/changelog.d/8454.bugfix @@ -0,0 +1 @@ +Fix several performance issues causing app non responsive issues. diff --git a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/SetReadMarkersTask.kt b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/SetReadMarkersTask.kt index 8e7592a8b4..5c44931009 100644 --- a/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/SetReadMarkersTask.kt +++ b/matrix-sdk-android/src/main/java/org/matrix/android/sdk/internal/session/room/read/SetReadMarkersTask.kt @@ -18,6 +18,8 @@ package org.matrix.android.sdk.internal.session.room.read import com.zhuinden.monarchy.Monarchy import io.realm.Realm +import kotlinx.coroutines.withContext +import org.matrix.android.sdk.api.MatrixCoroutineDispatchers import org.matrix.android.sdk.api.session.events.model.LocalEcho import org.matrix.android.sdk.api.session.homeserver.HomeServerCapabilitiesService import org.matrix.android.sdk.internal.database.model.RoomSummaryEntity @@ -64,9 +66,10 @@ internal class DefaultSetReadMarkersTask @Inject constructor( private val globalErrorReceiver: GlobalErrorReceiver, private val clock: Clock, private val homeServerCapabilitiesService: HomeServerCapabilitiesService, + private val coroutineDispatchers: MatrixCoroutineDispatchers, ) : SetReadMarkersTask { - override suspend fun execute(params: SetReadMarkersTask.Params) { + override suspend fun execute(params: SetReadMarkersTask.Params) = withContext(coroutineDispatchers.io) { val markers = mutableMapOf() Timber.v("Execute set read marker with params: $params") val latestSyncedEventId = latestSyncedEventId(params.roomId) diff --git a/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteUnusedClientInformationUseCase.kt b/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteUnusedClientInformationUseCase.kt index dcd5c58480..cc5dc6725d 100644 --- a/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteUnusedClientInformationUseCase.kt +++ b/vector/src/main/java/im/vector/app/core/session/clientinfo/DeleteUnusedClientInformationUseCase.kt @@ -17,6 +17,7 @@ package im.vector.app.core.session.clientinfo import im.vector.app.core.di.ActiveSessionHolder +import kotlinx.coroutines.withContext import org.matrix.android.sdk.api.session.crypto.model.DeviceInfo import javax.inject.Inject @@ -27,16 +28,19 @@ class DeleteUnusedClientInformationUseCase @Inject constructor( suspend fun execute(deviceInfoList: List): Result = runCatching { // A defensive approach against local storage reports an empty device list (although it is not a seen situation). if (deviceInfoList.isEmpty()) return Result.success(Unit) - - val expectedClientInfoKeyList = deviceInfoList.map { MATRIX_CLIENT_INFO_KEY_PREFIX + it.deviceId } - activeSessionHolder - .getSafeActiveSession() - ?.accountDataService() - ?.getUserAccountDataEventsStartWith(MATRIX_CLIENT_INFO_KEY_PREFIX) - ?.map { it.type } - ?.subtract(expectedClientInfoKeyList.toSet()) - ?.forEach { userAccountDataKeyToDelete -> - activeSessionHolder.getSafeActiveSession()?.accountDataService()?.deleteUserAccountData(userAccountDataKeyToDelete) - } + val dispatcher = activeSessionHolder.getSafeActiveSession()?.coroutineDispatchers?.io + ?: return@runCatching + withContext(dispatcher) { + val expectedClientInfoKeyList = deviceInfoList.map { MATRIX_CLIENT_INFO_KEY_PREFIX + it.deviceId } + activeSessionHolder + .getSafeActiveSession() + ?.accountDataService() + ?.getUserAccountDataEventsStartWith(MATRIX_CLIENT_INFO_KEY_PREFIX) + ?.map { it.type } + ?.subtract(expectedClientInfoKeyList.toSet()) + ?.forEach { userAccountDataKeyToDelete -> + activeSessionHolder.getSafeActiveSession()?.accountDataService()?.deleteUserAccountData(userAccountDataKeyToDelete) + } + } } } diff --git a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListViewModel.kt b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListViewModel.kt index 74b55d435d..fad1ad613d 100644 --- a/vector/src/main/java/im/vector/app/features/home/room/list/RoomListViewModel.kt +++ b/vector/src/main/java/im/vector/app/features/home/room/list/RoomListViewModel.kt @@ -322,11 +322,10 @@ class RoomListViewModel @AssistedInject constructor( } private fun handleDeleteLocalRooms() { - val localRoomIds = session.roomService() - .getRoomSummaries(roomSummaryQueryParams { roomId = QueryStringValue.Contains(RoomLocalEcho.PREFIX) }) - .map { it.roomId } - - viewModelScope.launch { + viewModelScope.launch(session.coroutineDispatchers.io) { + val localRoomIds = session.roomService() + .getRoomSummaries(roomSummaryQueryParams { roomId = QueryStringValue.Contains(RoomLocalEcho.PREFIX) }) + .map { it.roomId } localRoomIds.forEach { session.roomService().deleteLocalRoom(it) } diff --git a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt index 2d799034d9..61cb14a70f 100644 --- a/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt +++ b/vector/src/main/java/im/vector/app/features/notifications/NotificationDrawerManager.kt @@ -24,7 +24,9 @@ import im.vector.app.R import im.vector.app.core.resources.BuildMeta import im.vector.app.core.utils.FirstThrottler import im.vector.app.features.displayname.getBestName +import im.vector.app.features.session.coroutineScope import im.vector.app.features.settings.VectorPreferences +import kotlinx.coroutines.launch import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.content.ContentUrlResolver import org.matrix.android.sdk.api.session.getUserOrDefault @@ -121,11 +123,15 @@ class NotificationDrawerManager @Inject constructor( * Used to ignore events related to that room (no need to display notification) and clean any existing notification on this room. */ fun setCurrentRoom(roomId: String?) { - updateEvents { - val hasChanged = roomId != currentRoomId - currentRoomId = roomId - if (hasChanged && roomId != null) { - it.clearMessagesForRoom(roomId) + val dispatcher = currentSession?.coroutineDispatchers?.io ?: return + val scope = currentSession?.coroutineScope ?: return + scope.launch(dispatcher) { + updateEvents { + val hasChanged = roomId != currentRoomId + currentRoomId = roomId + if (hasChanged && roomId != null) { + it.clearMessagesForRoom(roomId) + } } } } @@ -135,12 +141,16 @@ class NotificationDrawerManager @Inject constructor( * Used to ignore events related to that thread (no need to display notification) and clean any existing notification on this room. */ fun setCurrentThread(threadId: String?) { - updateEvents { - val hasChanged = threadId != currentThreadId - currentThreadId = threadId - currentRoomId?.let { roomId -> - if (hasChanged && threadId != null) { - it.clearMessagesForThread(roomId, threadId) + val dispatcher = currentSession?.coroutineDispatchers?.io ?: return + val scope = currentSession?.coroutineScope ?: return + scope.launch(dispatcher) { + updateEvents { + val hasChanged = threadId != currentThreadId + currentThreadId = threadId + currentRoomId?.let { roomId -> + if (hasChanged && threadId != null) { + it.clearMessagesForThread(roomId, threadId) + } } } }