Home: getting Room interface slow (blocking Main Thread). Maybe we should get this async.

This commit is contained in:
ganfra 2019-02-27 17:41:06 +01:00
parent adc51529f2
commit 2c0bc93f5a
2 changed files with 18 additions and 4 deletions

View File

@ -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)
}


View File

@ -34,11 +34,25 @@ internal fun Monarchy.tryTransactionAsync(transaction: (realm: Realm) -> Unit):
}
}

fun <T : RealmModel> Monarchy.fetchManaged(query: (Realm) -> T?): T? {
return fetch(query, false)
}

fun <T : RealmModel> Monarchy.fetchCopied(query: (Realm) -> T?): T? {
return fetch(query, true)
}

private fun <T : RealmModel> Monarchy.fetch(query: (Realm) -> T?, copyFromRealm: Boolean): T? {
val ref = AtomicReference<T>()
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()
}
}