Как лучше всего использовать Singleton
с несколькими LiveData
для наблюдения и публикации?
Объект:
object EventsObj {
private val _actionLiveData = MutableLiveData<...>()
val actionLiveData: LiveData<...> = _actionLiveData
fun postActionEvent(value: ...) {
_actionLiveData.postValue(value)
}
... //few more LiveDatas following the same logic
}
или кинжал:
@Singleton
class EventsClass
@Inject constructor() {
private val _actionLiveData = MutableLiveData<...>()
val actionLiveData: LiveData<...> = _actionLiveData
fun postActionEvent(value: ...) {
_actionLiveData.postValue(value)
}
... //few more LiveDatas following the same logic
}
Использование:
@Inject
lateinit var eventsClass: EventsClass
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
eventsClass.actionLiveData.observe(this, ...)
eventsClass.postActionEvent(...)
EventsObj.actionLiveData.observe(this, ...)
EventsObj.postActionEvent(...)
}