Ganfra's review: use RealmQueryLatch
This commit is contained in:
parent
4eab0a3704
commit
b437837809
|
@ -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,6 +34,8 @@ 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()
|
||||||
|
|
||||||
|
if (result.isEmpty()) {
|
||||||
result.addChangeListener(object : RealmChangeListener<RealmResults<E>> {
|
result.addChangeListener(object : RealmChangeListener<RealmResults<E>> {
|
||||||
override fun onChange(t: RealmResults<E>) {
|
override fun onChange(t: RealmResults<E>) {
|
||||||
if (t.isNotEmpty()) {
|
if (t.isNotEmpty()) {
|
||||||
|
@ -48,6 +45,10 @@ class RealmQueryLatch<E : RealmObject>(private val realmConfiguration: RealmConf
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
} else {
|
||||||
|
realm.close()
|
||||||
|
latch.countDown()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
handler.post(runnable)
|
handler.post(runnable)
|
||||||
latch.await()
|
latch.await()
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue