我们可以在观察者中使用观察者吗?

Can we use an observer in an observer?

大家好,我遇到了以下问题。我有一个数据库室,我有 2 个表 POST_TABLE 和 SELL_TABLE.

现在我需要从两个表中获取数据并添加到数组列表中。 我使用观察器跟踪 出售商品 何时加载,然后将其添加到数组列表中,我对 post 商品

我这样做是为了在回收站视图中显示两种不同的视图类型对象。

在 ViewModel 中一切都很简单,我可以访问数据库,接收销售和 post DAO 并初始化存储库。

在我看来,将观察者设为家庭片段中的观察者是错误的。

在最佳实践中我没有找到合适的答案。也许有人知道如何正确地从房间获取数据?

From HomeFragment

private fun initObserver() {
        viewModel.sellList.observe(viewLifecycleOwner, {
            viewModel.addSellItemsToArray()
            
            viewModel.postList.observe(viewLifecycleOwner, {
                viewModel.addPostItemToArray()
                
                initRecyclerView(viewModel.homeArrayList)
            })
        })
    }

From ViewModel

private val sellRepository: SellRepository
    private val postRepository: PostRepository

    var sellList: LiveData<List<Sell>>
    var postList: LiveData<List<Post>>
    private var _homeItem = MutableLiveData<ArrayList<HomeItem>>().apply {
        value = arrayListOf()
    }
    val homeItem get() = _homeItem

    val homeArrayList = arrayListOf<HomeItem>()


    init {
        val sellDao = MainDatabase.getDatabase(application).sellDao()
        val postDao = MainDatabase.getDatabase(application).postDao()

        sellRepository = SellRepository(sellDao)
        postRepository = PostRepository(postDao)

        sellList = sellRepository.getAllSellList()
        postList = postRepository.getPostList()

    }

    fun addSellItemsToArray() {
        for (sell in sellList.value!!) {
            val homeItem = HomeItem()
            homeItem.userName = sell.userName
            homeItem.userImage = sell.userImage
            homeItem.imagesArray = sell.imagesArray
            homeItem.desc = sell.desc
            homeItem.itemForSell = sell.itemForSell
            homeItem.price = sell.price
            homeItem.country = sell.country
            homeItem.city = sell.city
            homeItem.address = sell.address

            homeArrayList.add(homeItem)
        }
    }

    fun addPostItemToArray() {
        for (post in postList.value!!) {
            val homeItem = HomeItem()
            homeItem.userName = post.postedUserName
            homeItem.userImage = post.userUrl
            homeItem.imagesArray = arrayListOf(post.postUrl)
            homeItem.desc = post.commit
            homeItem.likesCount = post.likesCount

            homeArrayList.add(homeItem)
        }
    }

It seems to me I'm doing wrong to make the observer in the observer in home fragment.

是的,有更好的方法来处理这种情况。

下面是其中之一:

class YourViewModel() : ViewModel() {
  private val tempHomeList = mutableListOf<HomeItem>()

  private val _homeList = MutableLiveData<HomeItem>()
  val homeList: LiveData<HomeItem> = _homeList

  private lateinit var sellRepository: SellRepository
  private lateinit var postRepository: PostRepository

  init {
    val sellDao = MainDatabase.getDatabase(application).sellDao()
    val postDao = MainDatabase.getDatabase(application).postDao()

    sellRepository = SellRepository(sellDao)
    postRepository = PostRepository(postDao)
  }

  fun initList() {
    addSellItemsToArray()
    addPostItemToArray()
    _homeList.value = tempHomeList
  }

  private fun addSellItemsToArray() {
    val sellList = sellRepository.getAllSellList()
    for (sell in sellList.value!!) {
      val homeItem = HomeItem()
      homeItem.userName = sell.userName
      homeItem.userImage = sell.userImage
      homeItem.imagesArray = sell.imagesArray
      homeItem.desc = sell.desc
      homeItem.itemForSell = sell.itemForSell
      homeItem.price = sell.price
      homeItem.country = sell.country
      homeItem.city = sell.city
      homeItem.address = sell.address

      tempHomeList.add(homeItem)
    }
  }

  private fun addPostItemToArray() {
    val postList = postRepository.getPostList()
    for (post in postList.value!!) {
      val homeItem = HomeItem()
      homeItem.userName = post.postedUserName
      homeItem.userImage = post.userUrl
      homeItem.imagesArray = arrayListOf(post.postUrl)
      homeItem.desc = post.commit
      homeItem.likesCount = post.likesCount

      tempHomeList.add(homeItem)
    }
  }
}

然后在您的 Fragment 中执行如下操作:

class YourFragment : Fragment {
 
  private val recyclerAdapter = YourRecyclerAdapter()

  override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
  ): View {
    //init your views, view model and your recycler view adapter
    //...
    recyclerView.adapter = recyclerAdapter
  }
  
  //...
  override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    viewModel.homeList.observe(viewLifecycleOwner, { list ->
      recyclerViewAdapter.updateList(list)
    })
    //start observing other liveData or do anything else here
    //....
    viewModel.initList()
  }
}

还有您的适配器示例...

class YourRecyclerAdapter : RecyclerView.Adapter<HomeItemAdapter.ViewHolder> {
  private val homeList = mutableListOf<HomeItem>()

  fun updateList(list: List<HomeItem>) {
    homeList.clear()
    homeList.addAll(list)
    notifyDataSetChanged()
  }

  //.... the rest of your adapter and viewholder
}