我希望在手术后取消订阅Rx Singles。 这就是我在RxJava中取消订阅Observable的方法。
SVN
但我不确定如何取消订阅Single Observable。 如果有人能够分享一些想法会很棒。
public class BooksRepositoryImpl implements IBooksRepository {
private ReplaySubject<Book> bookSubject;
private Subscription bookSubscription;
BooksApi BooksApi;
ISchedulerProvider scheduler;
@Inject
public BooksRepositoryImpl(BooksApi BooksApi, ISchedulerProvider scheduler) {
this.BooksApi = BooksApi;
this.scheduler = scheduler;
}
@Override
public Observable<Book> getBooks(String author) {
if (bookSubscription == null || bookSubscription.isUnsubscribed()) {
bookSubscription = ReplaySubject.create();
bookSubscription = BooksApi.getBooks(author)
.subscribeOn(scheduler.backgroundThread());
}
return bookSubscription.asObservable();
}
@Override
public void unbind() {
if (bookSubscription != null && !bookSubscription.isUnsubscribed())
bookSubscription.unsubscribe();
}
}
}
答案 0 :(得分:2)