Skip to content

Instantly share code, notes, and snippets.

@raghavsatyadev
Created March 9, 2025 11:19
Show Gist options
  • Save raghavsatyadev/bffbbd2891587f6864381c7ea0742700 to your computer and use it in GitHub Desktop.
Save raghavsatyadev/bffbbd2891587f6864381c7ea0742700 to your computer and use it in GitHub Desktop.
Bug in Room DB for date field

I'm encountering a weird bug with Room Database.

I have two fields in my model: localUpdateDateTime and serverUpdateDateTime, which I use for syncing data with the cloud.

The issue is that I am not saving the local time to the Firebase server, so I need to update it every time there's a write operation from the code or when retrieving data from the cloud. When I fetch data from the cloud, I simply set the local date equal to the server date.

However, I’ve noticed that when I debug right before inserting or updating the data in the Room Database, both dates appear to be the same. But after I add this data to Room, there seems to be a difference between values.

codeTime=Sun Mar 09 16:41:15 GMT+05:30 2025 dbLocal=Sun Mar 09 16:41:15 GMT+05:30 2025 (There is a slight diff in milliseconds) dbServer=Sun Mar 09 16:41:14 GMT+05:30 2025

Below is the code for the class where both the fields are located.

package io.github.raghavsatyadev.support.models.db.match_record

import android.os.Parcelable
import androidx.annotation.Keep
import androidx.room.ColumnInfo
import androidx.room.Embedded
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.firebase.firestore.Exclude
import com.google.firebase.firestore.PropertyName
import io.github.raghavsatyadev.support.Constants.DB.Tables
import io.github.raghavsatyadev.support.Constants.FieldKeys
import kotlinx.parcelize.Parcelize
import kotlinx.serialization.Contextual
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.util.Date

@Keep
@Parcelize
@Serializable
@Entity(tableName = Tables.MATCH_RECORD_TABLE)
data class MatchRecord(
    @SerialName(FieldKeys.MATCH_RECORD_ID)
    @ColumnInfo(FieldKeys.MATCH_RECORD_ID)
    @get:PropertyName(FieldKeys.MATCH_RECORD_ID)
    @set:PropertyName(FieldKeys.MATCH_RECORD_ID)
    @PrimaryKey
    var matchRecordId: String = "",

    @SerialName(FieldKeys.START_DATE_TIME)
    @ColumnInfo(FieldKeys.START_DATE_TIME)
    @get:PropertyName(FieldKeys.START_DATE_TIME)
    @set:PropertyName(FieldKeys.START_DATE_TIME)
    var startDateTime: Long,

    @SerialName(FieldKeys.END_DATE_TIME)
    @ColumnInfo(FieldKeys.END_DATE_TIME)
    @get:PropertyName(FieldKeys.END_DATE_TIME)
    @set:PropertyName(FieldKeys.END_DATE_TIME)
    var endDateTime: Long = 0L,

    @Embedded(prefix = "${FieldKeys.TEAM_1}_")
    @SerialName(FieldKeys.TEAM_1)
    @get:PropertyName(FieldKeys.TEAM_1)
    @set:PropertyName(FieldKeys.TEAM_1)
    var team1Detail: TeamDetail,

    @Embedded(prefix = "${FieldKeys.TEAM_2}_")
    @SerialName(FieldKeys.TEAM_2)
    @get:PropertyName(FieldKeys.TEAM_2)
    @set:PropertyName(FieldKeys.TEAM_2)
    var team2Detail: TeamDetail,

    @SerialName(FieldKeys.BALLS_PER_INNING)
    @ColumnInfo(FieldKeys.BALLS_PER_INNING)
    @get:PropertyName(FieldKeys.BALLS_PER_INNING)
    var ballsPerInning: Int,

    @SerialName(FieldKeys.DID_TEAM_1_WON_TOSS)
    @ColumnInfo(FieldKeys.DID_TEAM_1_WON_TOSS)
    @get:PropertyName(FieldKeys.DID_TEAM_1_WON_TOSS)
    @set:PropertyName(FieldKeys.DID_TEAM_1_WON_TOSS)
    var didTeam1WonToss: Boolean = true,

    @SerialName(FieldKeys.IS_TEAM_1_BATTING_FIRST)
    @ColumnInfo(FieldKeys.IS_TEAM_1_BATTING_FIRST)
    @get:PropertyName(FieldKeys.IS_TEAM_1_BATTING_FIRST)
    @set:PropertyName(FieldKeys.IS_TEAM_1_BATTING_FIRST)
    var isTeam1BattingFirst: Boolean = true,

    @SerialName(FieldKeys.IS_FIRST_INNING_COMPLETE)
    @ColumnInfo(FieldKeys.IS_FIRST_INNING_COMPLETE)
    @get:PropertyName(FieldKeys.IS_FIRST_INNING_COMPLETE)
    @set:PropertyName(FieldKeys.IS_FIRST_INNING_COMPLETE)
    var isFirstInningComplete: Boolean = false,

    @SerialName(FieldKeys.RRR_AT_SECOND_INNING_START)
    @ColumnInfo(FieldKeys.RRR_AT_SECOND_INNING_START)
    @get:PropertyName(FieldKeys.RRR_AT_SECOND_INNING_START)
    @set:PropertyName(FieldKeys.RRR_AT_SECOND_INNING_START)
    var rrrAtSecondInningStart: String = "",

    @SerialName(FieldKeys.STATUS)
    @ColumnInfo(FieldKeys.STATUS)
    @get:PropertyName(FieldKeys.STATUS)
    @set:PropertyName(FieldKeys.STATUS)
    var status: MatchStatus = MatchStatus.NOT_STARTED,

    @SerialName(FieldKeys.LOCATION)
    @ColumnInfo(FieldKeys.LOCATION)
    @get:PropertyName(FieldKeys.LOCATION)
    @set:PropertyName(FieldKeys.LOCATION)
    var location: String = "",

    @SerialName(FieldKeys.MATCH_ADMIN_ID)
    @ColumnInfo(FieldKeys.MATCH_ADMIN_ID)
    @get:PropertyName(FieldKeys.MATCH_ADMIN_ID)
    @set:PropertyName(FieldKeys.MATCH_ADMIN_ID)
    var matchAdminID: String,

    @SerialName(FieldKeys.MATCH_SHARED_USER_IDS)
    @ColumnInfo(FieldKeys.MATCH_SHARED_USER_IDS)
    @get:PropertyName(FieldKeys.MATCH_SHARED_USER_IDS)
    @set:PropertyName(FieldKeys.MATCH_SHARED_USER_IDS)
    var matchSharedUserIDs: List<String> = emptyList(),

    @SerialName(FieldKeys.LOCAL_UPDATE_DATE_TIME)
    @ColumnInfo(FieldKeys.LOCAL_UPDATE_DATE_TIME)
    @get:Exclude
    @Contextual
    var localUpdateDateTime: Date? = null,

    @SerialName(FieldKeys.SERVER_UPDATE_DATE_TIME)
    @ColumnInfo(FieldKeys.SERVER_UPDATE_DATE_TIME)
    @get:PropertyName(FieldKeys.SERVER_UPDATE_DATE_TIME)
    @set:PropertyName(FieldKeys.SERVER_UPDATE_DATE_TIME)
    @Contextual
    var serverUpdateDateTime: Date? = null,
) : Parcelable
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment