简体   繁体   English

如何在rxjava中添加公共观察者

[英]How to add a common observer in rxjava

I am pretty new to rxjava and just started learning it and converting our android project to rx. 我是rxjava的新手,刚开始学习它并将我们的android项目转换为rx。 So I wrote observables and chained them using flatmaps to call apis and return an observable. 因此,我编写了可观察对象,并使用平面图将它们链接起来以调用api并返回一个可观察对象。 Now in the main activity I subscribe to them and add my UI code to onNext, onSubscribe, onComplete and onError as applicable. 现在,在主要活动中,我订阅它们并将UI代码添加到onNext,onSubscribe,onComplete和onError(如果适用)。 But I have mutiple apis for which I have created an observable for each api, but I want to have a wrapper over the observer with which I subscribe to them. 但是我有多个api,我已经为每个api创建了一个可观察的api,但是我希望在我订阅它们的观察者上有一个包装。 It is mainly to execute a piece of code like logging onNext/onError calls and showing a progress status on onSubscribe and dismissing it on onComplete/onError etc. 它主要是执行一段代码,例如登录onNext / onError调用并在onSubscribe上显示进度状态,然后在onComplete / onError等上将其关闭。

How do I do this other than adding it each time or having a baseobserver and adding super.method each time (one may forget super.method hence this handling should be removed from activity code I guess) 除了每次添加它或拥有一个baseobserver并每次添加super.method之外,我该如何做(一个人可能会忘记super.method,因此应该从活动代码中删除此处理)

The idea is to implement a BaseActivity where all other activities extends from it. 这个想法是实现一个BaseActivity ,其中所有其他活动都将从其扩展。 In this activity, you declare your generic Observer and when extending from this class, you get that Observer and you can make your own modifications depending on your needs. 在此活动中,您声明常规的Observer并在从此类扩展时,获得该Observer,并且可以根据需要进行自己的修改。 Take a look at this code 看一下这段代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM