Reputation: 171
I am querying my room database to check if an item exists, however the query always returns null even though the item is already in the DB. I am using coroutines
This is my query
@Query("SELECT EXISTS(SELECT * FROM cart_item WHERE productId = :productId)")
suspend fun getItemCount(productId: Int): Int?
The function in my repository
suspend fun getCartItemCount(productId: Int): Int? {
return coroutineScope{
cartItemDao.getItemCount(productId)
}
}
In my view model
fun getCartItemCount(productId: Int): MutableLiveData<Int>? {
var itemCount: MutableLiveData<Int>? = MutableLiveData()
launch {
itemCount!!.value = repository.getCartItemCount(productId)
}
return itemCount
}
And this is how i implement it in my Fragment
fun getCartItemCount(productId: Int){
var itemCount: Int? = null
mainViewModel!!.getCartItemCount(productId)!!.observe(viewLifecycleOwner, Observer {
itemCount = it
})
Log.d("ITEMCOUNT ----> ", " $itemCount")
}
Upvotes: 1
Views: 1130
Reputation: 1767
I think you are missing some fundamentals on how to use coroutines.
itemCount
while still null. The trigger never did, and even if it does, it won't execute your log statement.Your view model uses LiveData
to post changes, then we do you need to return a value on your method?
Suggested Changes
Repository
// repository
suspend fun getCartItemCount(productId: Int): Int? {
return cartItemDao.getItemCount(productId)
}
View Model
var itemCount: MutableLiveData<Int> = MutableLiveData()
// maybe rename method as it's not a getter anymore
fun getCartItemCount(productId: Int) {
viewModelScope {
itemCount.value = repository.getCartItemCount(productId)
}
}
In your fragment
fun getCartItemCount(productId: Int){
mainViewModel?.observe(viewLifecycleOwner, Observer {
itemCount = it
// this will be triggered every time the "LiveData.value" changes
// this could return null if the live data value is not set.
Log.d("ITEMCOUNT", "$it")
})
mainViewModel?.getCartItemCount(productId)
}
Suggested Reading:
Upvotes: 2