1 . 先上我写的一个方法:
public static Observable<RxEvent> sendBytesRx(ByteBuffer buffer) {
writeThread.add(buffer);
return RxBus.getDefault().toObserverable(RxEvent.class);
}
这是一个发送数据到服务器的一个函数。
2 . 然后我就调用:
@OnClick(R.id.button2)
public void btn2(Button button) {
BgService.sendBytesRx(CSAPIUtils._1_LogIn(xxx,xxx))
.observeOn(Schedulers.newThread())
.compose(this.bindUntilEvent(ActivityEvent.DESTROY))
.subscribeOn(Schedulers.newThread())
.subscribe(
rxEvent -> {
//解析数据
LogUtils.e(OpenLog,TAG,"1");
},throwable -> {
LogUtils.e(OpenLog,TAG,throwable.getMessage());
});
}
3 . 发现有点不对,
当我多次点击这个Button,触发OnClick方法,发现,
第一次点击,返回数据,打印:1
第二次点击,返回数据,打印:1 1
...
好像是每次点击的时候,就订阅了这个RxBus的事件,但是
事件完成之后却没有去取消订阅...
请教各位大神!刚入Rx大门。
Subscribe multiple times and each subscriber will receive the message.
Solution: Only subscribe once during onCreate, or cancel the current subscription after receiving the message
There shouldn’t be any big problems with the second paragraph of code, except for the specification of Schedulers. The main problem lies in the first paragraph. Let’s take a look at the first paragraph of code:
RxBus.getDefault().toObserverable(RxEvent.class)
This should be the problem, it depends on the specific implementation code.p.s. It has nothing to do with the subscription timing. If it does, it is a problem with your own code logic. Rx subscription can be done at any time.