I'm trying to implement a timer Observable
that is Shared between the Activities
of my application. I am making the implementation on a class that is a Dagger singleton which I inject in each Presenter
of each different Activity
.
I create the Observable once, that way:
Observable.defer(() -> Observable.timer(milliseconds, TimeUnit.MILLISECONDS).map(t -> this::doSomethingCool()))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.share();
I make the subscribe from the Presenter with the function:
public Observable<Status> register(Callback callback) {
PublishSubject<Status> subject = PublishSubject.create();
subject.subscribe(status -> {},
throwable -> L.LOGE(TAG, throwable.getMessage()),
() -> callback.onStatusChanged(mBasketStatus));
mObservable.subscribe(subject);
basketCounterCallback.onStatusChanged(status));
subject.doOnUnsubscribe(() -> L.LOGD(TAG, "Unsubcribed from subject!"));
return subject.asObservable();
}
I store that Subject
as Observable
in every presenter, and I call to:
obs.unsubscribeOn(AndroidSchedulers.mainThread())
to unsubscribe (in onPause()
method). I also tried to unsubscribe using the Scheduler Schedulers.immediate()
But the callback is called anyway X times (where X is all the Presenters that I have subscribed to the timer), so it is not unsubscribing. Also the Log "Unsubcribed from subject!"
is not getting called.
How can I unsubscribe correctly from every subject?
Thanks in advance
EDIT:
Added more implementation details due to the comments:
This is the part where I create the Observable and store in a member of the Singleton
class StatusManager
(the status is also a singleton):
private Observable<BasketStatus> mObservable;
private Status mStatus;
public Observable<BasketStatus> start(long milliseconds, Status status, Callback callback) {
if (mObservable == null) mObservable = createObservable(milliseconds, status);
return register(callback);
}
private Observable<BasketStatus> createObservable(long milliseconds, Status status) {
mStatus = status;
return Observable.defer(() -> Observable.timer(milliseconds, TimeUnit.MILLISECONDS).map(t -> status.upgradeStatus()))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.share();
}
public Observable<BasketStatus> register(Callback callback) {
PublishSubject<Status> subject = PublishSubject.create();
subject.subscribe(status -> {},
throwable -> L.LOGE(TAG, throwable.getMessage()),
() -> callback.onStatusChanged(mStatus));
mObservable.subscribe(subject);
callback.onStatusChanged(mStatus));
subject.doOnUnsubscribe(() -> L.LOGD(TAG, "Unsubcribed from subject!"));
return subject.asObservable();
}
After to call to the method start(...)
from the Presenter
that start the timer I call to register(...)
method from the next Presenters:
class Presenter implements Callback {
private Observable<BasketStatus> mRegister;
@Inject
public Presenter(Status status, StatusManager statusManager) {
mRegister = statusManager.start(20000, status, this);
}
// Method called from onPause()
public void unregisterFromBasketStatus() {
mRegister.unsubscribeOn(Schedulers.immediate());
}
}
And the next presenter...
@Inject
public NextPresenter(StatusManager statusManager) {
mBasketStatusManager.register(this);
}