public void loadData1() {
repository.getData1();
}
public void loadData2() {
repository.getData2();
}
}
Type1Repository负责提供livedata变量比如mLiveData1、mLiveData2,已经具体获取数据的方法如getData1、getData2;
public class Type1Repository extends BaseRepository {
protected MutableLiveData mLiveData1;
protected MutableLiveData mLiveData2;
public LiveData getLiveData1() {
if (mLiveData1 == null) {
mLiveData1 = new MutableLiveData<>();
}
return mLiveData1;
}
public LiveData getLiveData2() {
if (mLiveData2 == null) {
mLiveData2 = new MutableLiveData<>();
}
return mLiveData2;
}
public void getData1() {
Observable.create((ObservableOnSubscribe) emitter -> {
try {
Thread.sleep(2000); // 假设此处是耗时操作
} catch (Exception e) {
e.printStackTrace();
emitter.onError(new RuntimeException());
}
emitter.onNext(true);
}
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Boolean orderValues) {
mLiveData1.setValue(orderValues);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public void getData2() {
…
}
}
方案2
ViewModel负责业务变量接口以及LiveData变量;
Repository负责获取数据,Repository和ViewModel之间如果涉及到异步调用问题,Repository的方法采用RxJava的Observable的返回值类型,返回给ViewModel调用方处理。
通过代码可以看到,Type2ViewModel生成了mLiveData1和mLiveData2变量,这些变量可以通过getLiveData1和getLiveData2供V层调用,并且提供了getLiveData1和getLiveData1方法。
public class Type2ViewModel extends BaseViewModel {
protected MutableLiveData mLiveData1;
protected MutableLiveData mLiveData2;
public Type2ViewModel(@NonNull Application application) {
super(application);
}
public LiveData getLiveData1() {
if (mLiveData1 == null) {
mLiveData1 = new MutableLiveData<>();
}
return mLiveData1;
}
public LiveData getLiveData2() {
if (mLiveData2 == null) {
mLiveData2 = new MutableLiveData<>();
}
return mLiveData2;
}
public void loadData1() {
repository.getData1().subscribe(new Observer() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(Boolean orderValues) {
mLiveData1.setValue(orderValues);
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
public void loadData2() {
…
}
}
而Type2Repository的getData1和getData2由于异步处理数据,返回了Observable类型。
public class Type2Repository extends BaseRepository {
public Observable getData1() {
return Observable.create((ObservableOnSubscribe) emitter -> {
try {
Thread.sleep(2000); // 假设此处是耗时操作
} catch (Exception e) {
e.printStackTrace();
emitter.onError(new RuntimeException());
}
emitter.onNext(true);
}
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread());
}
public Observable getData2() {
…
}
}
方案3(推荐)
方案1和方案2都涉及到了LiveData类型的创建和使用,要么在ViewModel中创建,要么在Repository中创建。如果在Repository中创建,那么还得通过ViewModel让V层得到。
那么我们是否可以考虑将LiveData的创建和使用都统一管理起来呢,就像异步分发的EventBus这样。通过类似于EventBus这样的异步分发管理机制,我们可以在任意地方创建LiveData,并且可以在想要用到的地方获取到LiveData。
我们考虑使用单例和一个HashMap来实现,提供register和post功能:
public class TinyLiveBus {
private ConcurrentHashMap
private static volatile TinyLiveBus sTinyBus;
public static TinyLiveBus getInstance() {
if (sTinyBus == null) {
return sTinyBus = new TinyLiveBus();
}
return sTinyBus;
}
public MutableLiveData register(String key, Class clazz) {
if (!liveDatas.containsKey(key)) {
liveDatas.put(key, new MutableLiveData<>());
}
return (MutableLiveData) liveDatas.get(key);
}
public void post(String key, T value) {
if (liveDatas.containsKey(key)) {
MutableLiveData liveData = liveDatas.get(key);
liveData.postValue(value);
}
}
}
注:这里postValue表示在子线程和主线程里都可以使用,而setValue只能在主线程中使用。
在Activity中,我们通过TinyLiveBus.getInstance().register方法创建LiveData:
public class Type3Activity extends BaseMVVMActivity {
@Override
protected int getContentView() {
return R.layout.activity_type;
}
@Override
protected void init() {
Button btn = findViewById(R.id.button);
TinyLiveBus.getInstance()
.register(“one”, Boolean.class)
.observe(this, (Observer) bool -> btn.setText(bool ? “success” : “fail”));
Button btn2 = findViewById(R.id.button2);
TinyLiveBus.getInstance()
.register(“two”, String.class)
.observe(this, (Observer) string -> btn2.setText(string));
}
public void clickMe(View view) {
mViewModel.loadData1();
}
public void clickOther(View view) {
mViewModel.loadData2();
}
}
在Repository中,我们通过TinyLiveBus.getInstance().post()通过LiveData有更新:
public class Type3Repository extends BaseRepository {
public void getData1() {
Observable.create((ObservableOnSubscribe) emitter -> {
try {
Thread.sleep(2000); // 假设此处是耗时操作
} catch (Exception e) {
e.printStackTrace();
emitter.onError(new RuntimeException());
}
emitter.onNext(true);
}
)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer() {