I have two fragments and each of them have Publish Subject that on initialization call onNext(true) in onResume(). I want to combine these two Subjects and call some method in third class when they both return true. Do i need to use Observable for this? I cannot find appropriate operation, zip does not work because these are Subjects. How can i combine these?
A subject is both a Observer and a Observable., so you can use zip operator, even with a subject.
What you can do, is using zip
with filter
operator.
Subject<Boolean, Boolean> sub1 = PublishSubject.create();
Observable<Boolean> filter1 = sub1.filter((e) -> e); // filter only event is true
Subject<Boolean, Boolean> sub2 = PublishSubject.create();
Observable<Boolean> filter2 = sub2.filter((e) -> e); // filter only event is true
Observable.zip(filter1, filter2, (one, two) -> true).subscribe()
If you just want to know if each fragment is initialized, maybe you can emit fragment instead to get a contract like : if view emitted, then it's ready.
class Fragment1 {
Subject<Fragment, Fragment> sub1 = PublishSubject.create();
public void onResume() {
sub1.onNext(this);
}
}
class Fragment2 {
Subject<Fragment, Fragment> sub1 = PublishSubject.create();
public void onResume() {
sub1.onNext(this);
}
}
class MyView {
public onCreate() {
Observable.zip(sub1, sub2, (one, two) -> true).subscribe((b) -> // dosomething)
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.