Does addObserver require removeObserver if component destroyed?

I have a use case where I am using addObserver to dynamically add an observer to a component. I’m wondering if I need to pair this with a removeObserver call if:

  1. I only ever add the observer once, and I never try to “change” it or “switch it out”.
  2. I only need it to be ‘removed’ when the component is destroyed.

In short, will Ember effectively ‘remove’ the observer for me when the component is destroyed?

The use case is something like this:

init() {

(Yes, I know I could use in a property instead, but my real use case is more complicated and prevents this.)

Can I simply omit the removeObserver, and ember will effectively take care of this for me when the component is destroyed?

If not (that is, if I DO need to explicitly call removeObserver), what event should I call it on? There is a willDestroyElement event, but is that always equivalent to destroying the component? (That is, is it possible to have the DOM element destroyed, but not the component? For example, if the component is inside an {{if}} in an HBS file?)

Thanks in advance!

Bumping this topic.

It seems crazy to me that no-one is able to answer this question? Shouldn’t this be a known in the Ember world?

Thanks in advance for any insight!

Usually doing cleanup like that (cleaning up scheduled runloop tasks, for instance) is done in the willDestroy hook which is inherited from EmberObject I believe.

As for do you need to? I think since observers are strongly discouraged for the most part this isn’t a common need and only someone with pretty deep knowledge is going to be able to answer that but I guess my general feeling is better safe than sorry :man_shrugging: