The following code is based the project https://github.com/enpassio/Databinding
I hope to determine whether exist a record by id, so I write the following code.
I think my code is not good, is these a simple way to determine whether exist a record by id using Room?
Can I directly a SQl and return Booean with @Dao? If so, how can I do?
Code
@Dao
interface ToyDao {
@get:Query("SELECT * FROM toys")
val allToys: LiveData<List<ToyEntry>>
@Query("SELECT * FROM toys WHERE toyId = :id")
fun getChosenToy(id: Int): LiveData<ToyEntry>
...
}
class ToyRepository private constructor(private val mDatabase: ToyDatabase, private val mExecutors: AppExecutors) {
val toyList: LiveData<List<ToyEntry>>
get() = mDatabase.toyDao().allToys
fun getChosenToy(toyId: Int): LiveData<ToyEntry> {
return mDatabase.toyDao().getChosenToy(toyId)
}
fun isExistRecord(toyId: Int):Boolean{
val my=getChosenToy(toyId)
if (my.value==null) return false else return true
}
...
}