最初,我从网络中获取列表数据,并将其存储在数据库中。
这是一对多的关系。为此,我创建了两个表和一个关系数据类。
产品表:
@Entity(tableName = "Product")
data class Products (
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "id")
var id : Int = 0,
@ColumnInfo(name = "name")
var name : String? = null,
@ColumnInfo(name = "category_id")
var category_id : String? = null,
@ColumnInfo(name = "subcategory_id")
var subcategory_id : String? = null,
@ColumnInfo(name = "variants")
var variants : List<Variants> = listOf()
)变式表:
@Entity(tableName = "Variant")
data class Variants (
@PrimaryKey(autoGenerate = false)
@ColumnInfo(name = "id")
var id : Int = 0,
@ColumnInfo(name = "product_id", index = true)
var product_id : Int? = null,
@ColumnInfo(name = "measurement")
var measurement : String? = null,
@ColumnInfo(name = "price")
var price : String? = null
)带有变体的产品(两个表之间的关系)
data class ProductWithVariants(
@Embedded val product: Products,
@Relation(
parentColumn = "id",
entityColumn = "product_id"
)
val variants: MutableList<Variants>
)Dao :我使用查询获取ProductsWithVariants数据,并通过subcategory_id进行筛选。
@Transaction
@Query("SELECT * FROM Product WHERE subcategory_id=:subcat")
fun getProductWithVariants(subcat:Int): Flow<MutableList<ProductWithVariants>>ViewModel:
var productListN: Flow<MutableList<ProductWithVariants>> = flowOf(mutableListOf())
fun networkproductlist() = viewModelScope.launch(Dispatchers.IO) {
productist.collectLatest {
when (it) {
Resource.Empty -> {
Log.e("product", "" + "empty")
}
is Resource.Failure -> {
Log.e("product", "" + "failure")
}
Resource.Loading -> {
}
is Resource.Success -> {
val response = it.value
for (i in 0 until response.data.size) {
val product: Products = response.data.get(i)
for (j in 0 until product.variants.size) {
variants = product.variants.get(j)
}
}
productsDao.deleteAllProducts()
productsDao.insertProducts(response.data)
productsDao.insertVariants(variants!!)
val subcategoryid = SubCategoryList(response.data[0].subcategory_id!!.toInt())
productsDao.insertSubCat(subcategoryid)
productListN = productsDao.getProductWithVariants(response.data[0].subcategory_id!!.toInt())
Log.e("product","product in room viewmodel : $productListN")
}
}
}
}如前所述,获得了成功资源中的网络响应,并在相应的表中插入产品和变体,然后在数据库中插入产品子类别,然后按下一行的ProductWithVariants ()获得getProductWithVariants()列表。
现在的问题是-我总是得到kotlinx.coroutines.flow.SafeFlow@f06e8a9作为productlistN的响应。
主要活动:
lifecycleScope.launch(Dispatchers.Main) {
proadapter = ProductAdapter()
binding.ProductRecyclerView.apply {
adapter = proadapter
}
}
lifecycleScope.launch(Dispatchers.Main) {
viewModel.productListN.collect {
println("Received UserInfo List $it")
proadapter.setMutableArraylist(it)
proadapter.notifyDataSetChanged()
}
}发布于 2022-09-07 14:48:36
我在您的代码中发现了几个问题:
在
ViewModel中使用var productListN = ...,然后用新的对象productListN = productsDao.getProductWithVariants(response.data[0].subcategory_id!!.toInt())重新初始化它。您不应该重新初始化它,您必须使用相同的对象。您可以尝试使用StateFlow或SharedFlow来发出数据:val productListN: Flow<MutableList<ProductWithVariants>> = StateFlow(mutableListOf())
...
productListN.value = ...在lifecycleScope.launch { ... }函数之外,
Activity中初始化proadapter和binding.ProductRecyclerView,而为了收集Flow数据,只需使用launchproadapter = ProductAdapter()
binding.ProductRecyclerView.apply {
adapter = proadapter
}
lifecycleScope.launch {
viewModel.productListN.collect {
println("Received UserInfo List $it")
proadapter.setMutableArraylist(it)
proadapter.notifyDataSetChanged()
}
}https://stackoverflow.com/questions/73629412
复制相似问题