androidfirebasekotlinfirebase-realtime-database

Firebase doTransaction setValue of data class changes the members names


I'm using runTransaction method in my Android app as follows:

@SuppressLint("RestrictedApi")
fun sendTableMsg(move: PokerTableMsg?) {
    Log.d(TAG, "sendTableMsg $move")
    this.mMessage = move
    mTableMessagesRef.runTransaction(this)
}

override fun doTransaction(mutableData: MutableData): Transaction.Result {
    Log.d(TAG, "doTransaction /S$mTurnCounter - $mMessage")
    mutableData.child("/S$mTurnCounter").value = mMessage
    return Transaction.success(mutableData)
}

and I'm getting the data in different place. Although the data class is:

data class PokerTableMsg(
    val turnIndex: UInt = 0u,
    val userState: UInt = UserState.INVALID.value,
    val minRaise: UInt = 0u
)

but in the firebase terminal it looks like that: enter image description here

Why is it adding the -pVg5ArA suffix to each member?


Solution

  • Actually the answer is interesting, the problem is that firebase can not find properties to serialize in class kotlin.UInt. I tried to change doTransaction to:

    override fun doTransaction(mutableData: MutableData): 
    Transaction.Result {
        Log.d(TAG, "doTransaction - S$mTurnCounter - $mMessage")
        val message = mapOf(
            MIN_RAISE_KEY to mMessage.minRaise,
            TURN_IDX_KEY to mMessage.turnIndex,
            USER_STATE_KEY to mMessage.userState
            )
        mutableData.child("S$mTurnCounter").value = message
        return Transaction.success(mutableData)
    }
    

    and got:

    com.google.firebase.database.DatabaseException: Firebase Database error: User code called from the Firebase Database runloop threw an exception: com.google.firebase.database.DatabaseException: No properties to serialize found on class kotlin.UInt

    I think it's a bug in firebase way of parsing data classs with members of type kotlin.UInt.

    I changed doTransaction back to:

    override fun doTransaction(mutableData: MutableData): 
    Transaction.Result {
        Log.d(TAG, "doTransaction - S$mTurnCounter - $mMessage")
        mutableData.child("S$mTurnCounter").value = mMessage
        return Transaction.success(mutableData)
    }
    

    and PokerTableMsg to:

    data class PokerTableMsg(
    val turnIndex: Int = 0,
    val userState: Int = UserState.INVALID.value,
    val minRaise: Int = 0
    )
    

    And it fixed it.