不推荐使用LocalBroadcastManager.我应该用它代替什么? [英] LocalBroadcastManager has been deprecated. What I should use instead in its place?

查看:352
本文介绍了不推荐使用LocalBroadcastManager.我应该用它代替什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Android上的该项目中进行工作,其中一个方面需要具有前台服务的CountdownTimer.关于Stack Overflow的其他一些答案提到LocalBroadcastManager将适合我的需求.

I'm working on this project in Android in which an aspect requires a CountdownTimer with a foreground service. A few other answers on Stack Overflow mentioned that LocalBroadcastManager would be suitable for my needs.

但是,Android Developers中的文档提到它已被弃用.关于我应该使用它的地方有什么建议吗?该文档提到了有关使用LiveData的信息,但我想知道是否还有其他更简单的选择.

The documentation in Android Developers, however, mentions that it has been deprecated. Any suggestions on what I should use in its place? The documentation mentioned about using LiveData, but I was wondering if there are any easier alternatives.

推荐答案

LocalBroadcastManager 基本上是 greenrobot的EventBus 是一个受欢迎的选择( Guava也有一个(如果您已经在使用Guava)(但是Guava非常笨重,只能用于事件总线).

LocalBroadcastManager is basically an event bus with a lot of unnecessary ceremony around Intents and intent filters. So one replacement is easy, and functions quite similarly: you can use any event bus library. greenrobot's EventBus is a popular choice (here's a guide for it) and Guava also has one, if you're already using Guava (but Guava is pretty heavy to include just for an event bus).

但是事件总线也遭受与LocalBroadcastManager相同的问题,导致过时它被弃用:它是全球性的,不具有生命周期意识,并且随着您的应用程序变得越来越大,推理更改的影响变得更加困难参加一个活动.对于观察数据的情况, LiveData 可以解决此问题很好,因为它是生命周期感知的,所以您不会在错误的时间(例如,在设置View之前或在 onSaveInstanceState 之后)获得更改通知-但它可以在以下情况下传递更改通知:您又回到了正确的状态.它的作用域也更严格-分别访问每个LiveData片段,而不是(通常)为整个应用程序配备一个事件总线/LocalBroadcastManager.

But event buses suffer from the same problems that LocalBroadcastManager does that led to it being deprecated: it's global, it's not lifecycle-aware, and as your app gets larger, it becomes much more difficult to reason about the effects of a change to an event. For cases of observing data, LiveData solves this quite nicely because it's lifecycle-aware, so you won't get change notifications at the wrong time (like before your View is set up, or after onSaveInstanceState) - but it'll handle delivering the change notifications when you're in the right state again. It's also more tightly scoped - each piece of LiveData is accessed separately rather than having (typically) one event bus/LocalBroadcastManager for the entire app.

对于更多的是事件而不是要更改的数据的情况,有时可以将其转换为数据.考虑一下您是否具有登录"和注销"事件-您可以创建一个LiveData来存储一个用于已登录用户的帐户,并在用户注销时变为null.然后组件可以观察到这一点.

For cases where it's more of an event rather than a piece of data being changed, you can sometimes convert it to a piece of data. Consider if you have "login" and "logout" events - you could instead create a LiveData that stores an Account for logged-in users, and becomes null when the user is logged out. Components could then observe that.

在某些情况下,确实很难将其转换为可观察的数据(尽管我无法立即想到通常会与事件总线模式一起使用的任何示例).对于这些,请考虑编写自己的侦听器界面,类似于单击式侦听器的工作方式.

There are certainly cases where it really is difficult to convert it to a piece of observable data (though I can't immediately think of any examples that would typically be used with an event bus patten). For those, consider writing your own listener interface, similar to how on-click listeners work.

对于您的倒数计时器示例,我认为LiveData是一个非常简单的解决方案,它将比事件总线甚至LocalBroadcastManager都容易得多.您只需拥有计时器当前值的LiveData,然后从需要显示该值的任何内容中订阅它即可.

For your example of a countdown timer, I think LiveData is a pretty straightforward solution, and will be much easier than an event bus or even LocalBroadcastManager would be. You can just have a LiveData of the timer's current value, and subscribe to it from whatever needs to show the value.

这篇关于不推荐使用LocalBroadcastManager.我应该用它代替什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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