Android 房间查询永远不会 return 为空?
Android Room Query never return null?
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 给我一条消息,说它总是正确的。如果数据库中没有匹配的行,我认为它应该为空。
留言是"Condition 'dao.getLotto(gameNum) != null' is always 'true'"
Livedata的值(dao.getLotto(gameNum).value)
可能为null,但Livedata的实例不会为null。它类似于 List<Lotto>
,列表中可能没有任何 Lotto 实例,但 List 实例不为空 [考虑到列表已创建]
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 给我一条消息,说它总是正确的。如果数据库中没有匹配的行,我认为它应该为空。
留言是"Condition 'dao.getLotto(gameNum) != null' is always 'true'"
Livedata的值(dao.getLotto(gameNum).value)
可能为null,但Livedata的实例不会为null。它类似于 List<Lotto>
,列表中可能没有任何 Lotto 实例,但 List 实例不为空 [考虑到列表已创建]