将项目添加到LiveData列表时通知观察者 [英] Notify Observer when item is added to List of LiveData

查看:356
本文介绍了将项目添加到LiveData列表时通知观察者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

将项目添加到LiveData列表时,我需要获取Observer事件.但是据我了解,事件仅在我用新清单替换旧清单时收到. 例如,当我执行下一个操作时:

I need to get Observer event when item is added to List of LiveData. But as far as I understand event receives only when I replace the old list with new one. For example when I do the next:

list.value = mutableListOf(IssuePost(UserEntity(name, email, photoUrl), issueEntity))

观察者获取事件.但是,当我仅将项目添加到值时,Observer保持沉默. 您能给我一个建议,告诉我我该如何实现我的需求吗?

Observer gets event. But when I just add item to value, Observer is silent. Could you please give an advice how I can implement what I need?

推荐答案

在内部,LiveData会以版本号(将简单计数器存储为int形式)跟踪每个更改.调用setValue()会递增此版本,并使用新数据更新所有观察者(仅当观察者的版本号小于LiveData的版本时).

Internally, LiveData keeps track of each change as a version number (simple counter stored as an int). Calling setValue() increments this version and updates any observers with the new data (only if the observer's version number is less than the LiveData's version).

似乎开始此过程的唯一方法是调用setValue()或postValue().副作用是,如果LiveData的基础数据结构发生了变化(例如,将元素添加到Collection中),则不会发生任何将其传达给观察者的事情.

It appears the only way to start this process is by calling setValue() or postValue(). The side-effect is if the LiveData's underlying data structure has changed (such as adding an element to a Collection), nothing will happen to communicate this to the observers.

因此,将项目添加到列表后,必须调用setValue().我在下面提供了两种方法可以解决此问题.

Thus, you will have to call setValue() after adding an item to your list. I have provided two ways you could approach this below.

选项1

将列表保留在LiveData之外,并在列表内容更改时随时使用参考进行更新.

Keep the list outside of the LiveData and update with the reference any time the list contents change.

private val mIssuePosts = ArrayList<IssuePost>()
private val mIssuePostLiveData = MutableLiveData<List<IssuePost>>()

fun addIssuePost(issuePost: IssuePost) {
   mIssuePosts.add(issuePost)
   mIssuePostLiveData.value = mIssuePosts
}

选项2

每当列表内容更改时,通过LiveData跟踪列表并使用其自己的值更新LiveData.

Keep track of the list via the LiveData and update the LiveData with its own value whenever the list contents change.

private val mIssuePostLiveData = MutableLiveData<MutableList<IssuePost>>()

init {
   mIssuePostLiveData.value = ArrayList()
}

fun addIssuePost(issuePost: IssuePost) {
    mIssuePostLiveData.value?.add(issuePost)
    mIssuePostLiveData.value = mIssuePostLiveData.value
}

这两种解决方案中的任何一种都可以帮助您避免在每次修改当前列表时仅为了通知观察者而创建新列表.

Either of these solutions should help you from having to create a new list every time you modify the current list just to notify the observers.

更新:

我已经使用类似技术已有一段时间了,正如Gnzlt在他的回答中提到的那样,使用Kotlin扩展功能将LiveData分配给自身以简化代码.这实际上是选项2的自动化:)我建议这样做.

I've been using similar techniques for a while now as Gnzlt has mentioned in his answer to use a Kotlin extension function to assign the LiveData to itself to simplify the code. This is essentially Option 2 automated :) I would recommend doing that.

这篇关于将项目添加到LiveData列表时通知观察者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆