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

View File

@ -75,7 +75,12 @@ class UsersRepositoryImpl(private val usersDao: UsersDao) : UsersRepository {
} }
override fun setUserAsActiveWithId(id: Long): Single<Boolean> { 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 { override fun deleteUser(user: User): Int {