当前位置:网站首页>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,则可以修复出现的错误;
}
边栏推荐
猜你喜欢
随机推荐
我的第一篇博客
MongoDB安装流程心得:
MapReduce流程
uniapp小程序禁止遮罩弹窗下的页面滚动的完美解决办法
【c】小游戏---五子棋之井字棋雏形
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十四章)
宝塔搭建PHP自适应懒人网址导航源码实测
PHP版本切换:5.x到7.3
Eslint规则大全
jwt (json web token)
拥抱Jetpack之印象篇
HBuilderX 核心插件安装提示:“插件XXX下载失败,请检查网络”问题的解决办法
华为路由交换
MySQL知识总结 (九) 用户与用户权限管理
7.如何给RecyclerView添加Click和LongClick事件
主存储器(二)
C语言初级—水仙花数
ABP,kendo后台接口,新增,查询
mysql常用函数
Raj delivery notes - separation 第08 speak, speaking, reading and writing