当前位置:网站首页>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,则可以修复出现的错误;
}
边栏推荐
猜你喜欢
随机推荐
mysql
C语言一维数组练习——将m个元素移动到数组尾部
科创知识年度盛会,中国科创者大会8月6日首场开幕!
不可不知的反汇编相关知识
DataX 的使用
MapReduce流程
Kubernetes核心概念
主存储器(一)
重新学习编程day1 【初始c语言】【c语言编写出计算两个数之和的代码】
Flask request application context source code analysis
Spark_Core
C语言初级—用一角,两角,五角和一元组成3.5元有多少种组合方法
C语言初级—数组元素的增删改查
uniCloud 未能获取当前用户信息:30205 | 当前用户为匿名身份
Flink-独立集群/Yarn
C语言日记 3 常量
Introduction and use of Haystack
Using the cloud GPU + pycharm training model to realize automatic background run programs, save training results, the server automatically power off
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十一章)
YOLOv7 uses cloud GPU to train its own dataset









