Reputation: 1441
I am looking to unsubscribe the Rx Singles after the operation. This is how I have unsubscribed Observable in RxJava.
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();
}
}
But I am not sure how to unsubscribe the Single Observable. It would be great if someone can able to share some idea.
public class BooksRepositoryImpl implements IBooksRepository {
BooksApi BooksApi;
ISchedulerProvider scheduler;
@Inject
public BooksRepositoryImpl(BooksApi BooksApi, ISchedulerProvider scheduler) {
this.BooksApi = BooksApi;
this.scheduler = scheduler;
}
@Override
public Single<Book> getBooks(String author) {
return BooksApi.getBooks(author)
.subscribeOn(scheduler.backgroundThread());
}
@Override
public void unbind() {
//?? HOW Can I unsubscribe here
}
}
Upvotes: 1
Views: 1848
Reputation: 2209
According to the documentation Single, you should do nothing, the subscription end by itself:
A Single will call only one of these methods, and will only call it once. Upon calling either method, the Single terminates and the subscription to it ends.
Hope this helps. Sorry for my english.
Upvotes: 3