Просмотр исходного кода

WIP. replace loop to set current state of users by single UPDATE query

i was able to see that the loop was somehow interrupted during debugging which caused two users to have current =true

this should avoid the problem with the loop.

anyway, this doesn't seem to solve the issue completely as i was able to reproduce it again with the new solution. so maybe there are still more methods/scenarios which can cause this.

additionally, i managed to have all users to have current =false with this new query (while switching accounts very fast and often in ChooseAccountDialogFragment..)

Signed-off-by: Marcel Hibbe <dev@mhibbe.de>
Marcel Hibbe 2 лет назад
Родитель
Сommit
962b1d4e3a

+ 8 - 23
app/src/main/java/com/nextcloud/talk/data/user/UsersDao.kt

@@ -22,13 +22,11 @@
 
 package com.nextcloud.talk.data.user
 
-import android.util.Log
 import androidx.room.Dao
 import androidx.room.Delete
 import androidx.room.Insert
 import androidx.room.OnConflictStrategy
 import androidx.room.Query
-import androidx.room.Transaction
 import androidx.room.Update
 import com.nextcloud.talk.data.user.model.UserEntity
 import io.reactivex.Maybe
@@ -83,27 +81,14 @@ abstract class UsersDao {
     @Query("SELECT * FROM User WHERE username = :username AND baseUrl = :server")
     abstract fun getUserWithUsernameAndServer(username: String, server: String): Maybe<UserEntity>
 
-    @Transaction
-    @Suppress("Detekt.TooGenericExceptionCaught") // blockingGet() only throws RuntimeExceptions per rx docs
-    open fun setUserAsActiveWithId(id: Long): Boolean {
-        return try {
-            getUsers().blockingGet().forEach { user ->
-                user.current = user.id == id
-
-                Log.d(TAG, "xxxxxxxxxxxx")
-                Log.d(TAG, "setUserAsActiveWithId. user.username: " + user.username)
-                Log.d(TAG, "setUserAsActiveWithId. user.id: " + user.id)
-                Log.d(TAG, "setUserAsActiveWithId. user.current: " + user.current)
-                Log.d(TAG, "xxxxxxxxxxxx")
-
-                updateUser(user)
-            }
-            true
-        } catch (e: RuntimeException) {
-            Log.e(TAG, "Error setting user active", e)
-            false
-        }
-    }
+    @Query(
+        "UPDATE User " +
+            "SET current = CASE " +
+            "WHEN id == :id THEN 1 " +
+            "WHEN userId != :id THEN 0 " +
+            "END"
+    )
+    abstract fun setUserAsActiveWithId(id: Long): Int
 
     companion object {
         const val TAG = "UsersDao"

+ 6 - 1
app/src/main/java/com/nextcloud/talk/data/user/UsersRepositoryImpl.kt

@@ -75,7 +75,12 @@ class UsersRepositoryImpl(private val usersDao: UsersDao) : UsersRepository {
     }
 
     override fun setUserAsActiveWithId(id: Long): Single<Boolean> {
-        return Single.just(usersDao.setUserAsActiveWithId(id))
+        val amountUpdated = usersDao.setUserAsActiveWithId(id)
+        return if (amountUpdated > 0) {
+            Single.just(true)
+        } else {
+            Single.just(false)
+        }
     }
 
     override fun deleteUser(user: User): Int {