'Room database update creates unexpected UI side effects

When I simply Insert my data for the first time, all is well and when I reload my tiles, they get saved in the database and the tiles reload normally - see Animation 1.

enter image description here

When I try to update my tiles, I have this weird behaviour, like a loop - see Animation 2.

enter image description here

I am wondering what could be causing this. Here is my code:

@Entity (tableName = "saved_values_table")
data class SavedValues (
    @PrimaryKey()
    var index: Int,
    var visible: Boolean,
    var enabled: Boolean,
    var boardLetters: String,
    var boardLettersValue: Int
)

Dao:

 @Update
    suspend fun updateValues(values: SavedValues)

Repo:

suspend fun updateValues(savedValues: SavedValues) {
    savedValuesDao.updateValues(savedValues)
}

ViewModel:

   fun saveValues(index: Int, boardLetters: Array<Letter>) {
        val savedValues = readAllValues.value
        if (savedValues != null) {
            if (savedValues.isEmpty()) {
                addValues(
                    SavedValues(
                        index,
                        visible = true,
                        enabled = true,
                        boardLetters = boardLetters[index].name,
                        boardLettersValue = boardLetters[index].value
                    )
                )
            }
            else {
                updateValues(
                    SavedValues(
                        index,
                        visible = true,
                        enabled = true,
                        boardLetters = boardLetters[index].name,
                        boardLettersValue = boardLetters[index].value
                    )
                )
            }
        }
    }

What am I doing wrong? Thanks for any help!

UPDATE I am adding some more code here as the problem persists, contrary to what I said in my comment.

My whole Dao:

@Dao
interface SavedValuesDao {

    @Insert
    suspend fun addValues(values: SavedValues)

    @Query("SELECT * FROM saved_values_table")
    fun readAllValues(): LiveData<List<SavedValues>>

    @Query("SELECT boardLetters FROM saved_values_table")
    fun readBoardLetters(): LiveData<List<String>>

    @Query("SELECT boardLettersValue FROM saved_values_table")
    fun readBoardLettersValues (): LiveData<List<Int>>

    @Update
    suspend fun updateValues(values: SavedValues)

    @Query("DELETE FROM saved_values_table")
    suspend fun deleteAllValues()

}

My Database:

@Database(
    entities = [Word::class, SavedValues::class, SavedWords::class],
    version = 8,
    exportSchema = false
)
abstract class WordDatabase : RoomDatabase() {
    abstract fun wordDAO(): WordDao
    abstract fun savedValuesDao(): SavedValuesDao
}

My Repository:

val readAllValues: LiveData<List<SavedValues>> = savedValuesDao.readAllValues()
val readBoardLetters: LiveData<List<String>> = savedValuesDao.readBoardLetters()
val readBoardLettersValues: LiveData<List<Int>> =
    savedValuesDao.readBoardLettersValues()

suspend fun addValues(savedValues: SavedValues) {
    savedValuesDao.addValues(savedValues)
}

suspend fun updateValues(savedValues: SavedValues) {
    savedValuesDao.updateValues(savedValues)
}

suspend fun deleteAllValues() {
    savedValuesDao.deleteAllValues()
}

Thanks for any help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source