gpt4 book ai didi

android - 如何取消订阅 RxJava 中的单个 Observable

转载 作者:行者123 更新时间:2023-11-29 19:16:34 29 4
gpt4 key购买 nike

我希望在手术后取消订阅 Rx Singles。这就是我在 RxJava 中取消订阅 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();

}

}

但我不确定如何取消订阅 Single Observable。如果有人能够分享一些想法,那就太好了。

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
}

最佳答案

根据文档Single ,您什么都不应该做,订阅会自行结束:

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.

希望这对您有所帮助。对不起我的英语。

关于android - 如何取消订阅 RxJava 中的单个 Observable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43320607/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com