gpt4 book ai didi

java - 如何在两个 Observable 之间重用订阅者 (RxJava)

转载 作者:搜寻专家 更新时间:2023-11-01 01:21:02 24 4
gpt4 key购买 nike

为了不重复,我想在两个可观察对象之间重新使用一个 Subscriber 变量。你是如何做到这一点的?我下面的当前代码不起作用,因为订阅者使用一次后,它被取消订阅并且不再起作用。如果我 new Subscriber 而不是重复使用变量,我的订阅就可以工作。如果可能的话,我不想将相同的代码写两次。

public class HomePresenter extends BasePresenter<HomeView> {
ArticleRepo articleRepo;

@Inject
public HomePresenter(ArticleRepo articleRepo) {
this.articleRepo = articleRepo;
}

@Override
public void onCreate(@Nullable PresenterBundle bundle) {
super.onCreate(bundle);
}


public void onEvent(ArticleCategoryClickedEvent event) {
Timber.v("Adapter position clicked at position: '%d'", event.getAdapterPosition());
view.launchArticleActivity(event.getArticleCategory());
}

public void onEvent(SeabeeOnlineExternalLinkClickedEvent event) {

view.launchExternalLink(event.getSeabeeOnlineExternalLink());
}

public void loadArticleImages() {
articleRepo.getArticleBuckets()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);
}

public void refreshData() {
articleRepo.refreshAndSaveArticles()
.flatMap(new Func1<List<ArticleEntity>, Observable<List<ImageArticleCategoryEntity>>>() {
@Override
public Observable<List<ImageArticleCategoryEntity>> call(List<ArticleEntity> articleEntityList) {
return articleRepo.getArticleBuckets();
}
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(subscriber);

}

final Subscriber<List<ImageArticleCategoryEntity>> subscriber = new Subscriber<List<ImageArticleCategoryEntity>>() {
@Override
public void onCompleted() {
Timber.v("Loading article images complete!");
view.hideLoadingAnimation();
}

@Override
public void onError(Throwable e) {
Timber.e("Error loading article images", e);
Log.e("tag", "Error loading article images", e);
}

@Override
public void onNext(List<ImageArticleCategoryEntity> integerImageArticleCategoryEntityHashMap) {
view.loadArticleImages(integerImageArticleCategoryEntityHashMap);
}
};

最佳答案

Subscriber 不应重复使用。它不会工作,因为它是一个订阅,一旦取消订阅就完成了。

如果您想重用它,请改用 Observer

source

关于java - 如何在两个 Observable 之间重用订阅者 (RxJava),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30221802/

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