Android Room Query永不返回null吗?

问题描述 投票:0回答:1
class LottoRepository(private val dao: LottoDao) {

    suspend fun getLotto(gameNum: Int): LiveData<Lotto> {

        if (dao.getLotto(gameNum) != null) { // This line shows me a tooltip that says it's always true

        }

    }
}

@Dao
interface LottoDao {

    @Query("SELECT * FROM lotto_numbers_table WHERE game_num = :gameNum")
    fun getLotto(gameNum: Int): LiveData<Lotto>

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    fun insertLottos(vararg lottos: Lotto)
}

@Entity(tableName = "lotto_numbers_table")
data class Lotto (
    @PrimaryKey @Json(name = "drwNo") @ColumnInfo(name = "game_num")
    val gameNum: Int,
    @Json(name = "drwNoDate") val date: String,
    @Json(name = "drwtNo1") val num1: Int,
    @Json(name = "drwtNo2") val num2: Int,
    @Json(name = "drwtNo3") val num3: Int,
    @Json(name = "drwtNo4") val num4: Int,
    @Json(name = "drwtNo5") val num5: Int,
    @Json(name = "drwtNo6") val num6: Int,
    @Json(name = "bnusNo") val bonusNum: Int,
    @Json(name = "totSellamnt") val totalSale: Long,
    @Json(name = "firstWinamnt") val firstWinnerReward: Long,
    @Json(name = "firstPrzwnerCo") val firstWinnerCount: Int,
    @Json(name = "firstAccumamnt") val firstWinnerTotalReward: Long
)

这些是我的代码,我想做的是检查与gameNum匹配的行是否存在。但是,当我检查null值时,Android Studio会显示一条消息,提示它始终为真。我认为如果数据库中没有匹配的行,则应为null。

消息是“条件'dao.getLotto(gameNum)!= null'始终为'true'“]

android android-room android-architecture-components
1个回答
0
投票
Livedata (dao.getLotto(gameNum).value)的值可能为空,但Livedata的实例将不会为空。类似于List<Lotto>,列表中可能没有乐透实例,但列表实例不为空[考虑到列表已创建]
© www.soinside.com 2019 - 2024. All rights reserved.