'Room data inspector returns empty record even it shows data in the room database
I have an application where user can select a category of items, then i have to show these items which belongs to this category, and this is how i do that:
First of all i am using MVVM architecture I load the data from firebase, i save it in a room database and when the user select the category i pass its name to the view model using (view model factory) which initialises the repository using this query and there i call the dao to get the items from the database.
What happens is it shows nothing, the dao returns null and the observer on the live data doesn't get called, when i debug the db using data base inspector, it shows that the data exists but when i do queries, it gives me empty records
The dao code:-
@Dao
interface productDao {
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addProduct(product: Product):Long
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun addProducts(Products:MutableList<Product>):List<Long>
@Query("DELETE FROM Product")
fun Clean()
@Query("SELECT * FROM Product WHERE Category = :query")
fun load(query:String):LiveData<MutableList<Product>>
}
the view model:-
class ProductViewModel @AssistedInject constructor( private val RepositoryFactory: ProductDealerFactory,@Assisted query: String): ViewModel() {
private var Repository:ProductDealer = RepositoryFactory.build(query)
val Products : LiveData<MutableList<Product>> = Repository.products
fun setProducts(products:MutableList<Product>){
Log.d("PRODUCTPRD",""+products.size)
Repository.setProducts(products)
}
@dagger.assisted.AssistedFactory
interface AssistedFactory {
fun create(query: String): ProductViewModel
}
companion object {
fun provideFactory(
assistedFactory: AssistedFactory,
query:String
): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel?> create(modelClass: Class<T>): T {
return assistedFactory.create(query) as T
}
}
}
}
The repository
class ProductDealer @AssistedInject constructor(@ApplicationContext var cxt: Context, @Assisted var query:String, private val productDao: productDao) {
private val executorService: ExecutorService = Executors.newFixedThreadPool(4)
var products: LiveData<MutableList<Product>> = MutableLiveData<MutableList<Product>>()
init {
getProducts(query)
}
private fun getProducts(query:String){
var callable:Callable<LiveData<MutableList<Product>>> = Callable {
productDao.load(query)
}
var future = executorService.submit(callable)
}
fun setProducts(Products:MutableList<Product>){
var callable:Callable<List<Long>> = Callable {
productDao.Clean()
productDao.addProducts(Products)
}
executorService.submit (callable)
}
}
the repository factory
@AssistedFactory
interface ProductDealerFactory {
fun build(query:String):ProductDealer
}
The entity
@Entity
data class Product(
@ColumnInfo(name = "Name")var Name:String,
@ColumnInfo(name = "Category") var Category:String,
@ColumnInfo(name="Price") var Price:Double,
@Ignore var Image:Bitmap?,
@ColumnInfo(name="Quantity") var Quantity:Double,
@ColumnInfo(name="Company") var Company:String,
@ColumnInfo(name="Details") var Details:String) : Parcelable{
@PrimaryKey(autoGenerate = true) var uId:Long=0L
constructor(Name: String,Category: String,Price: Double,Quantity: Double,Company: String,Details: String):this(Name,Category,Price,null,Quantity,Company,Details)
constructor(parcel: Parcel) : this(
Name = parcel.readString().toString(),
Category = parcel.readString().toString(),
Price = parcel.readDouble(),
Image = parcel.readParcelable(Bitmap::class.java.classLoader),
Quantity = parcel.readDouble(),
Company = parcel.readString().toString(),
Details = parcel.readString().toString()
){
}
override fun writeToParcel(parcel: Parcel, flags: Int) {
parcel.writeString(Name)
parcel.writeString(Category)
parcel.writeDouble(Price)
parcel.writeParcelable(Image,flags)
parcel.writeDouble(Quantity)
parcel.writeString(Company)
parcel.writeString(Details)
}
override fun describeContents(): Int {
return 0
}
companion object CREATOR : Parcelable.Creator<Product> {
override fun createFromParcel(parcel: Parcel): Product {
return Product(parcel)
}
override fun newArray(size: Int): Array<Product?> {
return arrayOfNulls(size)
}
class ProductBuilder(){
private var Name=""
private var Category=""
private var Price:Double=0.0
private var Image: Bitmap? =null
private var Quantity=0.0
private var Company =""
private var Details =""
fun setName(Name:String){
this.Name = Name
}
fun setPrice(Price: Double){
this.Price = Price
}
fun setQuantity(Quantity: Double){
this.Quantity = Quantity
}
fun setCompany(Company: String){
this.Company = Company
}
fun setDetails(Details: String){
this.Details = Details
}
fun setImg(img:Bitmap){
this.Image = img
}
fun setCategory(Category: String){
this.Category = Category
}
fun build():Product{
return Product(Name,Category,Price,Image,Quantity,Company,Details)
}
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
