diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt index b14627899f..0b0bae8da7 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/DefaultRoomService.kt @@ -26,13 +26,13 @@ import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntity import im.vector.matrix.android.internal.database.model.RoomSummaryEntityFields import im.vector.matrix.android.internal.database.query.where -import im.vector.matrix.android.internal.util.fetchCopied +import im.vector.matrix.android.internal.util.fetchManaged internal class DefaultRoomService(private val monarchy: Monarchy, private val roomFactory: RoomFactory) : RoomService { override fun getRoom(roomId: String): Room? { - monarchy.fetchCopied { RoomEntity.where(it, roomId).findFirst() } ?: return null + monarchy.fetchManaged { RoomEntity.where(it, roomId).findFirst() } ?: return null return roomFactory.instantiate(roomId) } diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/Monarchy.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/Monarchy.kt index 809ba16bf0..2a0fc5d708 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/Monarchy.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/util/Monarchy.kt @@ -34,11 +34,25 @@ internal fun Monarchy.tryTransactionAsync(transaction: (realm: Realm) -> Unit): } } +fun Monarchy.fetchManaged(query: (Realm) -> T?): T? { + return fetch(query, false) +} + fun Monarchy.fetchCopied(query: (Realm) -> T?): T? { + return fetch(query, true) +} + +private fun Monarchy.fetch(query: (Realm) -> T?, copyFromRealm: Boolean): T? { val ref = AtomicReference() doWithRealm { realm -> - val result = query.invoke(realm)?.let { realm.copyFromRealm(it) } + val result = query.invoke(realm)?.let { + if (copyFromRealm) { + realm.copyFromRealm(it) + } else { + it + } + } ref.set(result) } return ref.get() -} \ No newline at end of file +}