问题1:查看商品,先是获取商品数据,然后获取评论,然后。。。
解决手段:concat
详解:
-
concat
:接收若干个Observables,将其组合并有序发射
代码
private void pay() {
Observable.concat(getGoods(), getComment()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread())
.subscribe(new Observer<Object>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull Object o) {
System.out.println(o.toString());
}
@Override
public void onError(@NonNull Throwable e) {
e.printStackTrace();
}
@Override
public void onComplete() {
}
});
}
private Observable<Object> getGoods() {
return Observable.create(new ObservableOnSubscribe<Object>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Object> e) throws Exception {
// 获取商品数据
System.out.println("获取商品数据中");
Thread.sleep(3000);
e.onNext("我是商品数据");
// 注意,必须调用onComlete,否则下一个Observable不会执行
e.onComplete();
}
});
}
private Observable<Object> getComment() {
return Observable.create(new ObservableOnSubscribe<Object>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Object> e) throws Exception {
// 获取商品评论
System.out.println("获取商品评论中");
Thread.sleep(3000);
e.onNext("我是商品评论");
e.onComplete();
}
});
}
结果展示
问题1.1:登录,获取userID,然后根据ID去请求。。。
解决手段:flatmap
详解:
private void flatmap() {
login().flatMap(new Function<User, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(@NonNull User user) throws Exception {
System.out.println("用户id" + user.uID);
return comment(user.uID);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Boolean>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull Boolean o) {
System.out.println("评论" + o);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
问题1.2:页面数据需要请求几个接口才能得到最终信息。。。
解决手段:zip
详解:
private void zip() {
Observable.zip(getComment(), getFromNet(), new BiFunction<Object, Integer, Object>() {
@Override
public Object apply(@NonNull Object o, @NonNull Integer integer) throws Exception {
System.out.println("中间合并分别是" + o.toString() + "和" + integer);
return o.toString() + integer;
}
}).subscribe(new Observer<Object>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull Object o) {
System.out.println("结果" + o);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
结论:concat和merge都是两个Observable组合,每发射一次就会回调一次,zip就是两个Observable组合等两个Observable都发射了才会回调
问题2:展示一份数据,数据来源有本地缓存以及网络获取,首先查看是否有缓存,没有则使用网络
解决手段:concat
、first
详解:first
:只取第一个发射的数据
代码
private void getData() {
Observable.concat(getFromCache(),getFromNet()).first(1).subscribeOn(Schedulers.io())
.subscribeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<Integer>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onSuccess(@NonNull Integer integer) {
}
@Override
public void onError(@NonNull Throwable e) {
}
});
}
private Observable<Integer> getFromCache() {
return Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Integer> e) throws Exception {
if (hasCache) {
e.onNext(0);
}
e.onComplete();
}
});
}
private Observable<Integer> getFromNet() {
return Observable.create(new ObservableOnSubscribe<Integer>() {
@Override
public void subscribe(@NonNull ObservableEmitter<Integer> e) throws Exception {
if (!hasCache) {
Thread.sleep(3000);
e.onNext(1);
}
e.onComplete();
}
});
}
问题3:注册页面,需要所有信息填写完整,才能点击注册
解决手段:combineLatest
详解:combineLatest()
作用于最近发射的数据项:如果Observable1发射了A并且Observable2发射了B和C,combineLatest()将会分组处理AB和AC
代码
Observable<CharSequence> observableName = RxTextView.textChanges(((TextView) findViewById(R.id.text)));
Observable<CharSequence> observablePwd = RxTextView.textChanges(((TextView) findViewById(R.id.text)));
Observable<Boolean> observableEnd = Observable.combineLatest(observableName, observablePwd, new BiFunction<CharSequence,CharSequence,Boolean>() {
@Override
public Boolean apply(@NonNull CharSequence charSequence, @NonNull CharSequence charSequence2) throws Exception {
return charSequence.length() > 0 && charSequence2.length() > 0;
}
});
问题4:数据源需要进行筛选或者除null等过滤操作,不需要在结果再进行筛选,更专注与业务
解决手段:filter
代码
private void filter() {
Observable.fromArray("123","145",null,"3456").filter(new Predicate<String>() {
@Override
public boolean test(@NonNull String s) throws Exception {
// 这里会过滤掉null值已经不是1开头的字符串
return !TextUtils.isEmpty(s) && s.startsWith("1");
}
}).subscribe(new Observer<String>() {
@Override
public void onSubscribe(@NonNull Disposable d) {
}
@Override
public void onNext(@NonNull String s) {
System.out.println(s);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}