Ganfra's review: use RealmQueryLatch

This commit is contained in:
Benoit Marty 2019-06-10 16:35:51 +02:00
parent 4eab0a3704
commit b437837809
2 changed files with 22 additions and 55 deletions

View File

@ -18,12 +18,7 @@ package im.vector.matrix.android.internal.database
import android.os.Handler import android.os.Handler
import android.os.HandlerThread import android.os.HandlerThread
import io.realm.Realm import io.realm.*
import io.realm.RealmChangeListener
import io.realm.RealmConfiguration
import io.realm.RealmObject
import io.realm.RealmQuery
import io.realm.RealmResults
import java.util.concurrent.CountDownLatch import java.util.concurrent.CountDownLatch
private const val THREAD_NAME = "REALM_QUERY_LATCH" private const val THREAD_NAME = "REALM_QUERY_LATCH"
@ -39,15 +34,21 @@ class RealmQueryLatch<E : RealmObject>(private val realmConfiguration: RealmConf
val runnable = Runnable { val runnable = Runnable {
val realm = Realm.getInstance(realmConfiguration) val realm = Realm.getInstance(realmConfiguration)
val result = realmQueryBuilder(realm).findAllAsync() val result = realmQueryBuilder(realm).findAllAsync()
result.addChangeListener(object : RealmChangeListener<RealmResults<E>> {
override fun onChange(t: RealmResults<E>) { if (result.isEmpty()) {
if (t.isNotEmpty()) { result.addChangeListener(object : RealmChangeListener<RealmResults<E>> {
result.removeChangeListener(this) override fun onChange(t: RealmResults<E>) {
realm.close() if (t.isNotEmpty()) {
latch.countDown() result.removeChangeListener(this)
realm.close()
latch.countDown()
}
} }
} })
}) } else {
realm.close()
latch.countDown()
}
} }
handler.post(runnable) handler.post(runnable)
latch.await() latch.await()

View File

@ -16,23 +16,16 @@
package im.vector.matrix.android.internal.session.room.create package im.vector.matrix.android.internal.session.room.create
import android.os.Handler
import android.os.HandlerThread
import arrow.core.Try import arrow.core.Try
import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams import im.vector.matrix.android.api.session.room.model.create.CreateRoomParams
import im.vector.matrix.android.api.session.room.model.create.CreateRoomResponse import im.vector.matrix.android.api.session.room.model.create.CreateRoomResponse
import im.vector.matrix.android.internal.database.RealmQueryLatch
import im.vector.matrix.android.internal.database.model.RoomEntity import im.vector.matrix.android.internal.database.model.RoomEntity
import im.vector.matrix.android.internal.database.model.RoomEntityFields import im.vector.matrix.android.internal.database.model.RoomEntityFields
import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.network.executeRequest
import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.session.room.RoomAPI
import im.vector.matrix.android.internal.task.Task import im.vector.matrix.android.internal.task.Task
import io.realm.Realm
import io.realm.RealmChangeListener
import io.realm.RealmConfiguration import io.realm.RealmConfiguration
import io.realm.RealmResults
import java.util.concurrent.CountDownLatch
private const val THREAD_NAME = "CREATE_ROOM_"
internal interface CreateRoomTask : Task<CreateRoomParams, String> internal interface CreateRoomTask : Task<CreateRoomParams, String>
@ -47,42 +40,15 @@ internal class DefaultCreateRoomTask(private val roomAPI: RoomAPI,
}.flatMap { createRoomResponse -> }.flatMap { createRoomResponse ->
val roomId = createRoomResponse.roomId!! val roomId = createRoomResponse.roomId!!
val latch = CountDownLatch(1)
// Wait for room to come back from the sync (but it can maybe be in the DB is the sync response is received before)
val handlerThread = HandlerThread(THREAD_NAME + hashCode())
handlerThread.start()
val handler = Handler(handlerThread.looper)
// TODO Maybe do the same code for join room request ? // TODO Maybe do the same code for join room request ?
handler.post { // Wait for room to come back from the sync (but it can maybe be in the DB is the sync response is received before)
val realm = Realm.getInstance(realmConfiguration) val rql = RealmQueryLatch<RoomEntity>(realmConfiguration) { realm ->
realm.where(RoomEntity::class.java)
if (realm.where(RoomEntity::class.java) .equalTo(RoomEntityFields.ROOM_ID, roomId)
.equalTo(RoomEntityFields.ROOM_ID, roomId)
.findAll()
.isEmpty()) {
val result = realm.where(RoomEntity::class.java)
.equalTo(RoomEntityFields.ROOM_ID, roomId)
.findAllAsync()
result.addChangeListener(object : RealmChangeListener<RealmResults<RoomEntity>> {
override fun onChange(t: RealmResults<RoomEntity>) {
if (t.isNotEmpty()) {
result.removeChangeListener(this)
realm.close()
latch.countDown()
}
}
})
} else {
realm.close()
latch.countDown()
}
} }
latch.await() rql.await()
handlerThread.quit()
return Try.just(roomId) return Try.just(roomId)
} }
} }