Hi i have a list of items that i retrieve from a data source and then i apply a map on the observable to store the data into ROOM.
It manages to add it to the table but when i try to retrieve a LiveData of it, It doesnt seem to notify my observer of the results.
There is definetly data in the table and my query works as i changed the return time from LiveData to simple a List and that worked perfectly fine
Here is my data class
@Entity
data class Item(
@PrimaryKey
val id:String,
val title: String,
val description: String,
val imgUrl: String,
val usageRules : List<String>,
)
Here is my DAO that exposes a func that add all the list of Items
@Dao
interface MyDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun saveAllItems(itemList: MutableList<Item>)
@Query("SELECT * FROM items")
fun getAllItems(): LiveData<MutableList<Item>>
}
My DB class
@Database(entities = arrayOf(Item::class), version = 1, exportSchema = false)
@TypeConverters(UsageRulesTypeConverter::class)
abstract class MyDatabase : RoomDatabase() {
abstract fun getProductDao(): MyDao
}
Below is how i insert data into the database:
@Inject
lateInt val database : MyDatabase
override fun getAllItems(): Single<LiveData<MutableList<Item>>> {
//retrieve new data using rertrofit
networkController.getItems().map { responseItems ->
saveAllItems(responseItems )
getItems()
}
@Transaction
fun saveAllItems(allItems: ItemsDataSource) {
database.getProductDao().saveAllItems(allItems.loadedItems)
database.getProductDao().saveAllItems(allItems.savedItems)
database.getProductDao().saveAllItems(allItems.expiredItems)
database.getProductDao().saveAllItems(allItems.unloadedItems)
}
fun getItems() : LiveData<MutableList<Item>>{
return database.getProductDao().getAllItems()
}
My data source retrieved 4 lists of Items that i then save all of them in one entity/table but LiveData doesnt notify my UI about it?
ViewModel:
override fun getUnloadedOffers(): LiveData<MutableList<ProductOffer>> {
if (!this::itemsLiveData.isInitialized) {
itemsLiveData= MutableLiveData()
//get data from network or database
itemDelegator.getItems()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ items->
itemsLiveData= items
})
}
return itemsLiveData
}
UI
viewModel = ViewModelProviders.of(this, viewModelFactory).get(MyViewModel::class.java)
viewModel.getItems().observe(this, Observer {
items->
items?.let {
adapter = SomeAdapter(items)
itemsRecyclerView.adapter = adapter
adapter.notifyDataSetChanged()
}
})
Does your function getUnloadedOffers()
return empty mutableList? I'm not sure if passing items
to itemsLiveData
is going to work, since observing LiveData is running on bg thread (if I'm not mistaken)
Try to use vararg
instead of List
when saving collection of data.
@Insert(onConflict = OnConflictStrategy.IGNORE)
fun saveAllItems(vararg itemList: Item)
And then you can call this method using *
(also known as Spread Operator
) on your list like this
saveAllItems(*responseItems.toTypedArray())
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.