mirror of
https://github.com/SchildiChat/SchildiChat-android.git
synced 2024-11-26 19:36:08 +03:00
Realm: avoid opening realm if not necessary in ReadReceiptsSummaryMapper
This commit is contained in:
parent
fb2ed9a7ac
commit
beeee7c84b
1 changed files with 19 additions and 8 deletions
|
@ -16,8 +16,11 @@
|
|||
|
||||
package org.matrix.android.sdk.internal.database.mapper
|
||||
|
||||
import io.realm.Realm
|
||||
import io.realm.RealmList
|
||||
import org.matrix.android.sdk.api.session.room.model.ReadReceipt
|
||||
import org.matrix.android.sdk.internal.database.RealmSessionProvider
|
||||
import org.matrix.android.sdk.internal.database.model.ReadReceiptEntity
|
||||
import org.matrix.android.sdk.internal.database.model.ReadReceiptsSummaryEntity
|
||||
import org.matrix.android.sdk.internal.database.model.RoomMemberSummaryEntity
|
||||
import org.matrix.android.sdk.internal.database.query.where
|
||||
|
@ -32,14 +35,22 @@ internal class ReadReceiptsSummaryMapper @Inject constructor(
|
|||
return emptyList()
|
||||
}
|
||||
val readReceipts = readReceiptsSummaryEntity.readReceipts
|
||||
|
||||
return realmSessionProvider.withRealm { realm ->
|
||||
readReceipts
|
||||
.mapNotNull {
|
||||
val roomMember = RoomMemberSummaryEntity.where(realm, roomId = it.roomId, userId = it.userId).findFirst()
|
||||
?: return@mapNotNull null
|
||||
ReadReceipt(roomMember.asDomain(), it.originServerTs.toLong())
|
||||
}
|
||||
// Avoid opening a new realm if we already have one opened
|
||||
return if (readReceiptsSummaryEntity.isManaged) {
|
||||
map(readReceipts, readReceiptsSummaryEntity.realm)
|
||||
} else {
|
||||
realmSessionProvider.withRealm { realm ->
|
||||
map(readReceipts, realm)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private fun map(readReceipts: RealmList<ReadReceiptEntity>, realm: Realm): List<ReadReceipt> {
|
||||
return readReceipts
|
||||
.mapNotNull {
|
||||
val roomMember = RoomMemberSummaryEntity.where(realm, roomId = it.roomId, userId = it.userId).findFirst()
|
||||
?: return@mapNotNull null
|
||||
ReadReceipt(roomMember.asDomain(), it.originServerTs.toLong())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue