最初我从网络中获取列表数据,并将其存储在房间数据库中
这是一对多关系。为此,我创建了两个表和一个关系数据类
产品表:
@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:我使用query获取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")
}
}
}
}
正如我之前所说,在成功资源中获得网络响应,并在相应的表中插入产品和变体。之后,我在数据库中插入产品子类别,并通过点击下一行的getProductWithVariants((获得ProductWithVariant列表。。
现在的问题是——我总是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()
}
}
我在您的代码中发现了几个问题:
- 在
ViewModel
中,使用var productListN = ...
,然后使用新对象productListN = productsDao.getProductWithVariants(response.data[0].subcategory_id!!.toInt())
重新初始化它。你不应该重新初始化它,你必须使用相同的对象。您可以尝试使用StateFlow
或SharedFlow
来发射数据:
val productListN: Flow<MutableList<ProductWithVariants>> = StateFlow(mutableListOf())
...
productListN.value = ...
- 在
Activity
中,在launch
函数之外初始化proadapter
和binding.ProductRecyclerView
,对于收集Flow
数据,只需使用lifecycleScope.launch { ... }
:
proadapter = ProductAdapter()
binding.ProductRecyclerView.apply {
adapter = proadapter
}
lifecycleScope.launch {
viewModel.productListN.collect {
println("Received UserInfo List $it")
proadapter.setMutableArraylist(it)
proadapter.notifyDataSetChanged()
}
}