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();
})
);