Skip to content

Commit

Permalink
在removeObserver的时候,如果livedata上没有observer,则删除这个livadata,以减少内存占用
Browse files Browse the repository at this point in the history
  • Loading branch information
liaohailiang committed Dec 27, 2018
1 parent 23a4c8a commit 9ae7b8c
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 4 deletions.
4 changes: 2 additions & 2 deletions live-event-bus-v2/demo/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ dependencies {
implementation 'io.reactivex:rxandroid:1.2.1'

//liveeventbus
// implementation project(':liveeventbus')
implementation 'com.jeremyliao:live-event-bus:1.0.0'
implementation project(':liveeventbus')
// implementation 'com.jeremyliao:live-event-bus:1.0.0'

//test
testImplementation 'junit:junit:4.12'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.jeremyliao.liveeventbus.helper.LiveEventBusTestHelper;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -290,4 +292,11 @@ public void run() {
});
}

@Test
public void testRemoveObserve() throws Exception {
Assert.assertTrue(LiveEventBusTestHelper.getLiveEventBusCount() > 0);
rule.getActivity().finish();
Thread.sleep(500);
Assert.assertTrue(LiveEventBusTestHelper.getLiveEventBusCount() == 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jeremyliao.liveeventbus.helper;

import com.jeremyliao.liveeventbus.LiveEventBus;

import java.lang.reflect.Field;
import java.util.Map;

/**
* Created by liaohailiang on 2018/12/27.
*/
public class LiveEventBusTestHelper {

private LiveEventBusTestHelper() {
}

public static int getLiveEventBusCount() {
try {
Field bus = LiveEventBus.class.getDeclaredField("bus");
bus.setAccessible(true);
Map map = (Map) bus.get(LiveEventBus.get());
return map.size();
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static LiveEventBus get() {

public synchronized <T> Observable<T> with(String key, Class<T> type) {
if (!bus.containsKey(key)) {
bus.put(key, new BusLiveEvent<>());
bus.put(key, new BusLiveEvent<>(key));
}
return (Observable<T>) bus.get(key);
}
Expand Down Expand Up @@ -78,8 +78,14 @@ public void run() {
}
}

@NonNull
private final String key;
private Handler mainHandler = new Handler(Looper.getMainLooper());

private BusLiveEvent(String key) {
this.key = key;
}

@Override
protected Lifecycle.State observerActiveLevel() {
return super.observerActiveLevel();
Expand All @@ -90,5 +96,13 @@ protected Lifecycle.State observerActiveLevel() {
public void postValueDelay(T value, long delay, TimeUnit unit) {
mainHandler.postDelayed(new PostValueTask(value), unit.convert(delay, unit));
}

@Override
public void removeObserver(@NonNull Observer<T> observer) {
super.removeObserver(observer);
if (!hasObservers()) {
LiveEventBus.get().bus.remove(key);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;

import com.jeremyliao.liveeventbus.helper.LiveEventBusTestHelper;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
Expand Down Expand Up @@ -290,4 +292,11 @@ public void run() {
});
}

@Test
public void testRemoveObserve() throws Exception {
Assert.assertTrue(LiveEventBusTestHelper.getLiveEventBusCount() > 0);
rule.getActivity().finish();
Thread.sleep(500);
Assert.assertTrue(LiveEventBusTestHelper.getLiveEventBusCount() == 0);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.jeremyliao.liveeventbus.helper;

import com.jeremyliao.liveeventbus.LiveEventBus;

import java.lang.reflect.Field;
import java.util.Map;

/**
* Created by liaohailiang on 2018/12/27.
*/
public class LiveEventBusTestHelper {

private LiveEventBusTestHelper() {
}

public static int getLiveEventBusCount() {
try {
Field bus = LiveEventBus.class.getDeclaredField("bus");
bus.setAccessible(true);
Map map = (Map) bus.get(LiveEventBus.get());
return map.size();
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static LiveEventBus get() {

public synchronized <T> Observable<T> with(String key, Class<T> type) {
if (!bus.containsKey(key)) {
bus.put(key, new BusMutableLiveData<>());
bus.put(key, new BusMutableLiveData<>(key));
}
return (Observable<T>) bus.get(key);
}
Expand Down Expand Up @@ -82,9 +82,16 @@ public void run() {
}
}

@NonNull
private final String key;
private Map<Observer, Observer> observerMap = new HashMap<>();
private Handler mainHandler = new Handler(Looper.getMainLooper());


private BusMutableLiveData(String key) {
this.key = key;
}

@Override
public void postValue(T value) {
mainHandler.post(new PostValueTask(value));
Expand Down Expand Up @@ -146,6 +153,9 @@ public void removeObserver(@NonNull Observer<T> observer) {
realObserver = observer;
}
super.removeObserver(realObserver);
if (!hasObservers()) {
LiveEventBus.get().bus.remove(key);
}
}

private void setLifecycleObserverMapSize(Lifecycle lifecycle, int size) {
Expand Down

0 comments on commit 9ae7b8c

Please sign in to comment.