How to unsubscribe to an RXjava call in a broadcast receiver

被刻印的时光 ゝ 提交于 2019-12-14 02:05:25

问题


I am using RXJava2 to send email within a broadcast receiver and I would like to know when I should unsubscribe to the event. The code is basically:

        getSmsMmsObservable()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(throwable -> Timber.e(throwable, "Error sending mail."))
            .map(smsMmsAddress1 -> {
                smsMmsAddress = smsMmsAddress1;
                return doInBackgroundSendEmail();
            })
            .map(stringSingle -> {
                mMsgResponse = stringSingle;
                this.done = true;
                return deleteFile();
            })
            .subscribe(success -> {
                if (success) {
                    Toast.makeText(context, "Message Status: " + mMsgResponse, Toast.LENGTH_LONG).show();
                }

            });

When do I unsubscribe (there is no onPause or onDestroy in a receiver) and how do I know when the onReceive is finished? The receiver is registered via manifest. I thought of doing a composite observable and then disposing of it in the subscribe() section.

    compositeDisposable.add(
    getSmsMmsObservable()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .doOnError(throwable -> Timber.e(throwable, "Error sending mail."))
            .map(smsMmsAddress1 -> {
                smsMmsAddress = smsMmsAddress1;
                return doInBackgroundSendEmail();
            })
            .map(stringSingle -> {
                mMsgResponse = stringSingle;
                this.done = true;
                return deleteFile();
            })
            .subscribe(success -> {
                if (success) {
                    Toast.makeText(context, "Message Status: " + mMsgResponse, Toast.LENGTH_LONG).show();
                }
                compositeDisposable.dispose();

            })
    );

回答1:


As pointed out here, you should not perform long tasks on broadcast receiver.
Broadcast Receivers don't have lifecycle, when your onReceive returns it considered done by the system and your entire process might be killable afterwards, it will returns immediately at your case as you performing async processing. (read more here https://developer.android.com/guide/components/broadcasts.html under "Effects on process state").

You should create a dedicated Service for this task and perform the email sending there, with Service you have clear lifecycle, and you should unsubscribe at onDestory(). also pay attention to limited background processing and internet access starting with Marshmallow and Doze.

In any case you shouldn't dispose at onNext/onCompleted, the Observable will trigger dispose logic when it's terminated (onCompleted/onError).



来源:https://stackoverflow.com/questions/45701147/how-to-unsubscribe-to-an-rxjava-call-in-a-broadcast-receiver

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!