当前位置:网站首页>It is not allowed to subscribe with a(n) xxx multiple times.Please create a fresh instance of xxx
It is not allowed to subscribe with a(n) xxx multiple times.Please create a fresh instance of xxx
2022-08-02 14:06:00 【慢行的骑兵】
- 记录一个关于rxjava的报错问题,原因(可能是原因之一):subscribe方法使用时,将Observer对象非正常的使用了多次。
io.reactivex.exceptions.ProtocolViolationException: It is not allowed to subscribe with a(n) com.x.y.activity.zzzActivity$1 multiple times. Please create a fresh instance of com.x.y.activity.zzzActivity$1 and subscribe that to the target source instead.
一、报错信息如下

二、关键信息
It is not allowed to subscribe with a(n) xxx.QuestionActivity$1 multiple times.
Please create a fresh instance of xxx.QuestionActivity$1 and subscribe that to the target source instead.
- 翻译:不允许使用(一个QuestionActivity$1)多次订阅。请创建一个新的(QuestionActivity$1实例)并将其订阅到目标源。
- 看到这里还是没有立刻找出原因,然后就花了几分钟百度一下,没有找到最直接的答案,然后就去查看报错原因。于是又去看报错代码,发现了同一个RxBaseSubscriber(自己封装的类,实现了DisposableObserver,DisposableObserver继承了Observer)使用了两次。
- 捋一下RxBaseSubscriber被使用两次的原因,在QuestionActivity中需要访问两个接口(这里不考虑架构问题和rxjava的flatMap操作符),串联访问的,在请求第一个接口的时候创建了RxBaseSubscriber对象rxBaseSubscriber1,访问成功继续请求第二个接口,创建了RxBaseSubscriber对象rxBaseSubscriber2,在第二个接口使用subscribe方法时把rxBaseSubscriber1传入进去了导致的问题。
//错误代码
private RxBaseSubscriber mRxBaseSubscriber1;
private RxBaseSubscriber mRxBaseSubscriber2;
private void funcation1() {
mRxBaseSubscriber1 = new RxBaseSubscriber<String>() {
@Override
public void onError(APIException e) {
}
@Override
public void onSuccess(String code) {
funcation2(code);
}
};
RxUtils
.getInstance()
.//略...
.map(new RxFunction<String>())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mRxBaseSubscriber1);
}
private void funcation2(String code) {
mRxBaseSubscriber2 = new RxBaseSubscriber<List<JavaBean>>(){
@Override
public void onError(APIException e) {
//吐司
}
@Override
public void onSuccess(List<JavaBean> areaInfos) {
}
};
RxUtils
.getInstance()
.//略...
.map(new RxFunction<List<JavaBean>>())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(mRxBaseSubscriber1);
//将mRxBaseSubscriber1更改为mRxBaseSubscriber2,则可以修复出现的错误;
}
边栏推荐
猜你喜欢

Using the cloud GPU + pycharm training model to realize automatic background run programs, save training results, the server automatically power off

十分钟带你入门Nodejs

二级指针,数组指针,指针数组和函数指针

Raj delivery notes - separation 第08 speak, speaking, reading and writing

利用c语言实现对键盘输入的一串字符的各类字符的计数

mysql常用函数

C语言——一级指针初识

Kubernetes架构和组件

宝塔搭建DM企业建站系统源码实测

Flink前期代码结构
随机推荐
无人驾驶综述:摘要
C语言初级—判断一个数是不是素数(函数封装)
ConstraintLayout从入门到放弃
初识c语言指针
7.如何给RecyclerView添加Click和LongClick事件
主存储器(二)
二级指针,数组指针,指针数组和函数指针
spark写sql的方式
C语言初级—用一角,两角,五角和一元组成3.5元有多少种组合方法
MySQL知识总结 (六) MySQL调优
PHP版本切换:5.x到7.3
Tornado framework routing system introduction and (IOloop.current().start()) start source code analysis
Scala连接Mysql数据库
Camera Hal(Hal3)层修改Preview流
MySQL知识总结 (一) 数据类型
MarkDown syntax summary
C语言一维数组练习——将m个元素移动到数组尾部
c语言用scanf出错不安全的解决办法
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第九章)
MapReduce流程