如何创建发出单个事件并仅通知最后订阅的观察者的 LiveData?

How to create LiveData which emits a single event and notifies only last subscribed observer?

我创建了像 example.

中那样发出单个事件的实时数据

我的下一个问题是: 如何在 LiveData 中的值发生变化时仅通知最后订阅的观察者?

我想到的是将观察者存储在 SingleLiveData class 的链表中,然后仅当传递的观察者与列表的最后一个元素相同时才调用 super.observe

我不确定这是否是最佳方法。

我想使用此机制将 FAB 单击事件从 activity 传播到显示在 ViewPager 内部的片段。片段是动态添加到视图分页适配器的,所以假设我们知道片段的顺序。

最后,我找到了解决这个问题的方法。我不得不放弃发出单个事件的实时数据,因为它无法按照我需要的方式运行。

取而代之的是,我使用了简单的可变实时数据,它发出一个事件对象,该对象包装了一个数据,如 Jose Alcérreca article 的最后一段所述。

我在视图寻呼机中显示片段,所以我当时只有一个可见片段。

所以我的视图模型如下所示:

class ActionViewModel : ViewModel() {
  private val onCreateLiveData: MutableLiveData<Event<String>> = MutableLiveData()

  fun observeOnCreateEvent(): LiveData<Event<String>> = onCreateLiveData

  fun onCreateCollectionClick(message: String) {
    this.onCreateLiveData.value = Event(message)
  }
}

事件包装器 class 实现如下所示:

/*Used as a wrapper for data that is exposed via a LiveData that represents an 
 event.*/

open class Event<out T>(private val content: T) {

  var hasBeenHandled = false
    private set // Allow external read but not write

  /**
   * Returns the content and prevents its use again.
  */
  fun getContentIfNotHandled(): T? {
    return if (hasBeenHandled) {
      null
    } else {
      hasBeenHandled = true
      content
    }
  }

  /**
    * Returns the content, even if it's already been handled.
  */
  fun peekContent(): T = content
}

现在我们可以在片段中观察到这样的事件:

override fun onActivityCreated(savedInstanceState: Bundle?) {
   super.onActivityCreated(savedInstanceState)

   actionViewModel = ViewModelProviders.of(requireActivity()).get(ActionViewModel::class.java)
   actionViewModel.observeOnCreateEvent()
       .observe(this, Observer {
         it?.takeIf { userVisibleHint }?.getContentIfNotHandled()?.let {
           //DO what ever is needed
         }
       })
}
如果片段当前对用户可见,

Fragment userVisibleHint 属性 将 return 为真。因为我们当时只展示一个片段,所以这对我们有用。这意味着该片段将仅访问 事件 可见的数据。

此外,事件包装器的实现只允许读取一次值,因此每次 Observer 获取此事件时,它的值将为 null,我们将忽略它。

结论:通过这种方式,我们模拟了一个单一的事件实时数据,它只通知最后订阅的观察者。

我做了一个解决方案,有空看看 https://github.com/ueen/LiveEvent

如果您使用的是 Kotlin,则可以将 LiveData 替换为 Flow. StateFlow can be used to replace regular LiveData, while SharedFlow 可用于无状态事件。它还将为您提供空安全以及 Flow.

附带的所有运算符和配置

在其他地方 here 描述了迁移。这是一个基本示例:

视图模型:

interface MyViewModel {
    val myData: StateFlow<MyData>
    val myEvents: SharedFlow<MyEvent>
}

class MyViewModelImpl: MyViewModel {
    override val myData = MutableStateFlow(MyData())
    override val myEvents = MutableSharedFlow<MyEvent>(replay = 0, extraBufferCapacity = 1, BufferOverflow.DROP_OLDEST)

    /*
     * Do stuff 
     */
}

Activity:

lifecycleScope.launch {
    myData.collect {
        // handle stateful data    
    }
}

lifecycleScope.launch {
    myEvents.collect {
        // handle stateless events    
    }
}

请注意 lifecycleScope 需要适当的 ktx dependency

Herer's 在 Android.

中阅读更多关于 Flow 的内容