从可观察对象接收到第一个项目后,我试图取消订阅。它似乎不起作用。我究竟做错了什么?
public class ObservableAndSubscriber {
public static void main(final String[] args) {
final Observable<String> strObservable = Observable.create(s -> {
while (true) {
s.onNext("Hello World!!");
}
});
final Subscriber<String> strSubscriber = new Subscriber<String>() {
@Override
public void onCompleted() {
}
@Override
public void onError(final Throwable e) {
e.printStackTrace();
}
@Override
public void onNext(final String t) {
System.out.println(t);
this.unsubscribe();
}
};
strObservable.subscribe(strSubscriber);
}
}
结果似乎在无限循环中打印“Hello World”。