我正在使用rx java和android,我的事件总线类如下
public class EventBus {
private final Subject<Event, Event> subject = new SerializedSubject<>(PublishSubject.<Event>create());
private Observable<Map<Type, Event>> stickyObservable;
public EventBus() {
createStickyObservable();
}
private void createStickyObservable() {
final List<Observable<Event>> observables = new ArrayList<>();
final Observable<Map<Type, Event>> so = subject
.filter(event -> event.sticky)
.groupBy(event -> event.type)
.switchMap(groupedObservable -> {
BehaviorSubject<Event> bs = BehaviorSubject.create();
groupedObservable.subscribe(bs);
observables.add(bs);
return Observable.combineLatest(observables, args -> {
Map<Type, Event> map = new HashMap<>();
for (Object arg : args) {
Event event = (Event) arg;
map.put(event.type, event);
}
return map;
});
});
final BehaviorSubject<Map<Type, Event>> bs = BehaviorSubject.create();
so.subscribe(bs);
stickyObservable = bs;
}
public Observable<Event> filter(final String pathExpression) {
final Pattern pattern = Pattern.compile(pathExpression);
return subject.filter(event -> {
if (event.path == null) {
return pathExpression == null;
}
return pattern.matcher(event.path).matches();
});
}
public Observable<Map<Type, Event>> getStickyObservable() {
return stickyObservable;
}
public void event(Event event) {
subject.onNext(event);
}
}
有些时候我的应用程序崩溃了
rx.internal.operators.OperatorSwitch$SwitchSubscriber
当我检查问题时,似乎有一个
java.lang.ExceptionInInitializerError on the OperatorSwitch with
Caused by: java.lang.RuntimeException: java.lang.NoSuchFieldException: state