"object unsubscribed" when trying to subscribe to an EventEmitter again


#1

hello,
in my app i need to make sure that i unsubscribe to an EventEmitter when the page is changed, so i dont subscribe twice, when re-entering the page.
however when i try to subscribe again to the Emitter i get an “object unsubscribed” Error and when i already unsubscribed i get a promise execption in case the event fires.

is use the following code

in my provider i have:

private userDBlistener: EventEmitter<Object> = new EventEmitter<Object>();

//and later:

public getUserChangeListener():EventEmitter<Object>{
	  	return this.userDBlistener;
}

in my page component:

private subscription:Subscription;
private listener:EventEmitter<Object>;

//and then in the constructor:

this.listener=this.myProvider.getUserChangeListener();
this.subscription = this.listener.subscribe(result => {

})
//and on unload:
ionViewWillUnload () {
    this.listener.unsubscribe()
    this.subscription=null
}

where is my mistake?
thanks a lot!


#2

argh… found the solution…
it should be

this.subscription.unsubscribe();


#3

You might want to put your subscription into ionViewWillEnter() instead of the constructor. That way your page subscribes if a user reaches it by hitting the back button. Ionic destroys pages when the page is popped off the nav stack, but otherwise it tends to keep the pages alive, so your constructor won’t execute again.


#4

ah. thank you.
that is good intel!