首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >房间流总是返回kotlinx.coroutines.flow.SafeFlow@c8abe2e

房间流总是返回kotlinx.coroutines.flow.SafeFlow@c8abe2e
EN

Stack Overflow用户
提问于 2022-09-07 02:18:46
回答 1查看 112关注 0票数 0

最初,我从网络中获取列表数据,并将其存储在数据库中。

这是一对多的关系。为此,我创建了两个表和一个关系数据类。

产品表:

代码语言:javascript
复制
@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()
)

变式表:

代码语言:javascript
复制
@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
)

带有变体的产品(两个表之间的关系)

代码语言:javascript
复制
  data class ProductWithVariants(
    @Embedded val product: Products,
    @Relation(
        parentColumn = "id",
        entityColumn = "product_id"
    )
    val variants: MutableList<Variants> 
)

Dao :我使用查询获取ProductsWithVariants数据,并通过subcategory_id进行筛选。

代码语言:javascript
复制
@Transaction
@Query("SELECT * FROM Product WHERE subcategory_id=:subcat")
fun getProductWithVariants(subcat:Int): Flow<MutableList<ProductWithVariants>>

ViewModel:

代码语言:javascript
复制
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的响应。

主要活动:

代码语言:javascript
复制
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()
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-09-07 14:48:36

我在您的代码中发现了几个问题:

  • In ViewModel中使用var productListN = ...,然后用新的对象productListN = productsDao.getProductWithVariants(response.data[0].subcategory_id!!.toInt())重新初始化它。您不应该重新初始化它,您必须使用相同的对象。您可以尝试使用StateFlowSharedFlow来发出数据:

代码语言:javascript
复制
val productListN: Flow<MutableList<ProductWithVariants>> = StateFlow(mutableListOf())
...
productListN.value = ...

lifecycleScope.launch { ... }函数之外,

  • Activity中初始化proadapterbinding.ProductRecyclerView,而为了收集Flow数据,只需使用launch

代码语言:javascript
复制
proadapter = ProductAdapter()
binding.ProductRecyclerView.apply {
    adapter = proadapter
}

lifecycleScope.launch {
    viewModel.productListN.collect {
        println("Received UserInfo List $it")
        proadapter.setMutableArraylist(it)
        proadapter.notifyDataSetChanged()
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73629412

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档