diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/RealmQueryLatch.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/RealmQueryLatch.kt index 8db44148d3..700f54c778 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/RealmQueryLatch.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/database/RealmQueryLatch.kt @@ -18,12 +18,7 @@ package im.vector.matrix.android.internal.database import android.os.Handler import android.os.HandlerThread -import io.realm.Realm -import io.realm.RealmChangeListener -import io.realm.RealmConfiguration -import io.realm.RealmObject -import io.realm.RealmQuery -import io.realm.RealmResults +import io.realm.* import java.util.concurrent.CountDownLatch private const val THREAD_NAME = "REALM_QUERY_LATCH" @@ -39,15 +34,21 @@ class RealmQueryLatch(private val realmConfiguration: RealmConf val runnable = Runnable { val realm = Realm.getInstance(realmConfiguration) val result = realmQueryBuilder(realm).findAllAsync() - result.addChangeListener(object : RealmChangeListener> { - override fun onChange(t: RealmResults) { - if (t.isNotEmpty()) { - result.removeChangeListener(this) - realm.close() - latch.countDown() + + if (result.isEmpty()) { + result.addChangeListener(object : RealmChangeListener> { + override fun onChange(t: RealmResults) { + if (t.isNotEmpty()) { + result.removeChangeListener(this) + realm.close() + latch.countDown() + } } - } - }) + }) + } else { + realm.close() + latch.countDown() + } } handler.post(runnable) latch.await() diff --git a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt index 45c3c8b1ec..7ac8e83efc 100644 --- a/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt +++ b/matrix-sdk-android/src/main/java/im/vector/matrix/android/internal/session/room/create/CreateRoomTask.kt @@ -16,23 +16,16 @@ package im.vector.matrix.android.internal.session.room.create -import android.os.Handler -import android.os.HandlerThread 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.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.RoomEntityFields import im.vector.matrix.android.internal.network.executeRequest import im.vector.matrix.android.internal.session.room.RoomAPI import im.vector.matrix.android.internal.task.Task -import io.realm.Realm -import io.realm.RealmChangeListener import io.realm.RealmConfiguration -import io.realm.RealmResults -import java.util.concurrent.CountDownLatch - -private const val THREAD_NAME = "CREATE_ROOM_" internal interface CreateRoomTask : Task @@ -47,42 +40,15 @@ internal class DefaultCreateRoomTask(private val roomAPI: RoomAPI, }.flatMap { createRoomResponse -> 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 ? - handler.post { - val realm = Realm.getInstance(realmConfiguration) - - if (realm.where(RoomEntity::class.java) - .equalTo(RoomEntityFields.ROOM_ID, roomId) - .findAll() - .isEmpty()) { - val result = realm.where(RoomEntity::class.java) - .equalTo(RoomEntityFields.ROOM_ID, roomId) - .findAllAsync() - - result.addChangeListener(object : RealmChangeListener> { - override fun onChange(t: RealmResults) { - if (t.isNotEmpty()) { - result.removeChangeListener(this) - realm.close() - latch.countDown() - } - } - }) - } else { - realm.close() - latch.countDown() - } + // 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 rql = RealmQueryLatch(realmConfiguration) { realm -> + realm.where(RoomEntity::class.java) + .equalTo(RoomEntityFields.ROOM_ID, roomId) } - latch.await() - handlerThread.quit() + rql.await() + return Try.just(roomId) } }