Realm-js: Specific `removeListener` not working.

Created on 19 Jun 2016  路  2Comments  路  Source: realm/realm-js

With the following addListener and removeListener, the listener remains active (i.e. doesn't get removed):

Realm.addListener('change', this.realmChanged.bind(this));

Realm.removeListener('change', this.realmChanged.bind(this));

// A change to Realm will still trigger a call to realmChanged().

Am I using this correctly?

Thanks!

Josh

Most helpful comment

Great suggestion @joshuapinter. We'll do that! We're going to be redoing the docs for that very soon because we are expanding on where you can listen for events.

All 2 comments

Okay, I figured the .bind(this) was causing issues so I was able to get the following to work just fine:

this.realmChanged = this.realmChanged.bind(this);

Realm.addListener('change', this.realmChanged);

Realm.removeListener('change', this.realmChanged);

I'm not a JS expert so was that an obvious solution?

Also, it would be useful to put an example of removeListener in your non-API docs so others don't accidentally fall into a similar trap.

Thanks!

Great suggestion @joshuapinter. We'll do that! We're going to be redoing the docs for that very soon because we are expanding on where you can listen for events.

Was this page helpful?
0 / 5 - 0 ratings