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>
This commit is contained in:
Marcel Hibbe 2022-12-13 15:32:23 +01:00 committed by Andy Scherzinger
parent f73d1c14fc
commit 962b1d4e3a
No known key found for this signature in database
GPG key ID: 6CADC7E3523C308B
2 changed files with 14 additions and 24 deletions

View file

@ -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"

View file

@ -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 {