In order to not repeat myself, I want to re-use a Subscriber
variable between two observables. How do you do accomplish this? My current code below does not work, because after the subscriber is used once, it is unsubscribed and no longer works again. If I new
a Subscriber
instead of reusing a variable, my subscription works. I don't want to write the same code twice, if possible.
为了不重复自己,我想在两个observable之间重用一个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);
}
};
}
2 个解决方案
#1
A Subscriber
should not be reused. It will not work because it is a Subscription
and once unsubscribed it is done.
订阅者不应重复使用。它不起作用,因为它是一个订阅,一旦取消订阅它就完成了。
Use an Observer
instead if you want to reuse it.
如果要重用它,请使用Observer。
#2
You can reuse your subscriber, you just need to create an actual class out of it.
您可以重复使用您的订户,您只需要创建一个实际的类。
private static class MySubscriber extends Subscriber<List<ImageArticleCategoryEntity>> {...}
Subscriber<> subscriber1 = new MySubscriber();
Subscriber<> subscriber2 = new MySubscriber();
And there you go.
你去吧
#1
A Subscriber
should not be reused. It will not work because it is a Subscription
and once unsubscribed it is done.
订阅者不应重复使用。它不起作用,因为它是一个订阅,一旦取消订阅它就完成了。
Use an Observer
instead if you want to reuse it.
如果要重用它,请使用Observer。
#2
You can reuse your subscriber, you just need to create an actual class out of it.
您可以重复使用您的订户,您只需要创建一个实际的类。
private static class MySubscriber extends Subscriber<List<ImageArticleCategoryEntity>> {...}
Subscriber<> subscriber1 = new MySubscriber();
Subscriber<> subscriber2 = new MySubscriber();
And there you go.
你去吧