Comments (10)
我刚试了下,重写removeObserver可以解决这个问题,虽然看起来好像简单粗暴哈哈
@Override
public void removeObserver(@NonNull Observer<T> observer) {
super.removeObserver(observer);
if (!hasObservers()) {
LiveEventBus.bus.remove(eventKey);
}
}
from liveeventbus.
能否这样,在BusMutableLiveData内重写onInactive方法
protected void onInactive() {
super.onInactive();
if (!hasObservers()) {
LiveEventBus .bus.remove(eventKey);
}
}
当然,需要为BusMutableLiveData添加构造方法
public BusMutableLiveData(String eventKey){
this.eventKey = eventKey;
}
from liveeventbus.
这个map对象是一直持有的,据我所知,所有的bus类组件,如rxbus,eventbus都会一直持有,但是区别于缓存类组件,bus类组件一般持有的都是String,Integer等小对象,不会持有Bitmap等会占用大量内存的对象,所以不用太担心这个问题
from liveeventbus.
map集合里面也存了很多LiveData对象,也就是说项目了注册了多少,里面就存多少,没移除操作,这样是否友好,多了以后会不会影响其内存。
from liveeventbus.
谢谢你的建议,以后会考虑添加把没有订阅者的observable定期清理掉的功能
from liveeventbus.
嗯,我也是这样想的,在取消订阅或者类似onInactive的时机去检查当前Observer上面是否hasObservers,然后把这个Observer remove掉。这是个很好的思路,谢谢
from liveeventbus.
能否这样,在BusMutableLiveData内重写onInactive方法
protected void onInactive() { super.onInactive(); if (!hasObservers()) { LiveEventBus .bus.remove(eventKey); } }当然,需要为BusMutableLiveData添加构造方法
public BusMutableLiveData(String eventKey){ this.eventKey = eventKey; }
这种方式确实不错,我再想想有没有什么问题,赞一个
from liveeventbus.
这个方法刚刚验证了一下,是有问题的
onInactive回调时一般都仍然会有一个Observer,翻看源码发现
@Override
public void onStateChanged(LifecycleOwner source, Lifecycle.Event event) {
if (mOwner.getLifecycle().getCurrentState() == DESTROYED) {
removeObserver(mObserver);
return;
}
activeStateChanged(shouldBeActive());
}
在生命周期变化后,会回调onStateChanged方法,这里如果不为destroy,调用activeStateChanged方法
void activeStateChanged(boolean newActive) {
if (newActive == mActive) {
return;
}
// immediately set active state, so we'd never dispatch anything to inactive
// owner
mActive = newActive;
boolean wasInactive = LiveData.this.mActiveCount == 0;
LiveData.this.mActiveCount += mActive ? 1 : -1;
if (wasInactive && mActive) {
onActive();
}
if (LiveData.this.mActiveCount == 0 && !mActive) {
onInactive();
}
if (mActive) {
dispatchingValue(this);
}
}
在activeStateChanged方法中 只有原先的mActiviCount=0(即1变为0)且mActive为true时才回调onInactive
然而,activity在destroy之前会走onPause,此时就会走上面流程,并回调onInactive,所以这里调用hasObservers返回true
不好意思,之前的方案是错误的
不过应该有方案解决,我看看
from liveeventbus.
我刚试了下,重写removeObserver可以解决这个问题,虽然看起来好像简单粗暴哈哈
@Override public void removeObserver(@NonNull Observer<T> observer) { super.removeObserver(observer); if (!hasObservers()) { LiveEventBus.bus.remove(eventKey); } }
我看了LiveData的源码,确实这样是可行的。我准备使用你这种方法来弄。再次表示感谢:)
from liveeventbus.
这个问题已解决,感谢提出这个问题的小伙伴以及@GreenhairTurtle的解决方案,具体参见commit:9ae7b8c
from liveeventbus.
Related Issues (20)
- lifecycle-extensions包在2.2.0弃用
- android.os.TransactionTooLargeException: data parcel size 2600612 bytes HOT 3
- androidx 1.8.0 activity内viewpager2内两个fragment, activity发送,fragment收不到消息 HOT 3
- MVP 的 P层持有activity的 lifecycle引用使用Liveeventbus,activity销毁的时候,会引起内存泄漏吗?
- livedata有数据倒灌的问题啦
- 同一个key且同一个observer会重复订阅 HOT 1
- 哦?弃更了吗?
- 建议添加:支持线程切换
- 控制粘性事件多次发送(Event数据bean,每次bean都不一样),为什么值触发一次?可以每次都触发吗>?只会接受到最后一次的数据bean?
- 是否可以传递null
- LiveEventBus post 最大可以发送多少数据
- 事件被内部吞掉,导致不回调问题
- 3
- 不维护了嘛
- 请问下安卓U什么时候适配
- 大佬,能适配下Android14吗?
- 带生命周期的注册无法反注册
- 新系统适配 HOT 1
- 适配了Android14 HOT 17
- PostDelay问题 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from liveeventbus.