Problem 1: Viewing an item, first getting the item data, then the review, then…
The solution:
concat
: Receives several Observables, combines them and launches them in order
code
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 voidonComplete() {}}); } private Observable<Object>getGoods() {
returnObservable.create(new ObservableOnSubscribe<Object>() { @Override public void subscribe(@NonNull ObservableEmitter<Object> e) throws Exception {system.out.println (ObservableEmitter<Object> e)"Getting commodity data");
Thread.sleep(3000);
e.onNext("I'm commodity data"); // Note that onComlete must be called otherwise the next Observable will not execute e.oncomplete (); }}); } private Observable<Object>getComment() {
returnObservable.create(new ObservableOnSubscribe<Object>() { @Override public void subscribe(@NonNull ObservableEmitter<Object> e) throws Exception {system.out.println (ObservableEmitter<Object> e)"Get product reviews");
Thread.sleep(3000);
e.onNext("I'm a product review."); e.onComplete(); }}); }Copy the code
The results show
Problem 1.1: Log in, get userID, and request based on ID…
Solution: Flatmap
private void flatmap() {
login().flatMap(new Function<User, ObservableSource<Boolean>>() {
@Override
public ObservableSource<Boolean> apply(@NonNull User user) throws Exception {
System.out.println("User 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("Comment" + o);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {}}); }Copy the code
Problem 1.2: Page data needs to request several interfaces to get the final information…
Solution: 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("The intermediate merger is." + o.toString() + "And" + 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("The results" + o);
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {}}); }Copy the code
Conclusions: Both Concat and Merge are two Observable combinations, which will call back every time they launch. Zip is two Observable combinations, which will call back only when both observables launch
Problem 2: Show a piece of data from local cache and network. Check whether the data is cached. If no, use network
Best solution: Only take the data code of the first transmission
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();
}
});
}
Copy the code
Problem 3: On the registration page, you need to fill in all information before you can click register
Best Solution: combineLatest() works on the most recently launched data item: If Observable1 fires A and Observable2 fires B and C, combineLatest() will group AB and AC
code
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 {returncharSequence.length() > 0 && charSequence2.length() > 0; }});Copy the code
Problem 4: Data sources need to perform filtering operations, such as filtering or null-excluding. They do not need to perform filtering after the results. They are more focused on business
Solution: Filter code
private void filter() {
Observable.fromArray("123"."145",null,"3456").filter(new Predicate<String>() {
@Override
public boolean test(@nonNULL String s) throws Exception {// Filters out the String whose NULL value does not start with 1return! 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() {}}); }Copy the code