Our Publisher is subscribed to multiple times. If every subscriber sets a filter when subscribing to events, does this filter only affect this subscriber? Or does it affect all subscribers?
Let's test:
/**
* By: Liu Jian on July 11, 2018 17:22
* Email: 15313727484@163.com
*/
public class RxJava_filter implements Consumer<RxBus.Event>, Predicate<RxBus.Event> {
public static void main(String[] args) {
RxJava_filter rf = new RxJava_filter();
RxBus.get().toFlowable(RxBus.Event.class).filter(rf).subscribe(rf);
RxBus.get().post(2, 2);
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
RxBus.get().toFlowable(RxBus.Event.class).filter(new Predicate<RxBus.Event>() {
@Override
public boolean test(RxBus.Event e) throws Exception {
return false;
}
}).subscribe(new Consumer<RxBus.Event>() {
@Override
public void accept(RxBus.Event e) {
System.out.println(e.what + "I filtered you\n");
}
});
RxBus.get().post(2, 2);
}
}).start();
}
@Override
public void accept(RxBus.Event e) throws Exception {
System.out.println(e.object + "I show you\n");
}
@Override
public boolean test(RxBus.Event e) throws Exception {
if (e.what == 2) return true;
return false;
}
}
Test results:
2I show you
2I show you
Process finished with exit code 0
Test ideas:
Once the program has started, add a subscriber and send the event, we have the first print: 2 I show you
Then every five seconds, we add another subscriber, set a filter at the same time, and then publish the event again
So we had a second print: "2 I show you“
But the subscriber we set later didn't print it. It was filtered out. The subscriber we set earlier can print
Conclusion:
Each time the filter is set, it will only affect the subscribers of this time, and other subscribers will not be impressed.