question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot remove a result object's listener

See original GitHub issue

Goals

I’m trying to update my UI based on changes in to a realm object, so I added a listener to the result that calls forceUpdate. This solution, while a little ugly since I don’t like calling forceUpdate, works correctly, but when I call removeListener on the collection, it still continues to call forceUpdate. I’ve also tried calling removeListener on the entire realm, as well as removeAllListeners on the result object and entire realm with no success.

I know its a no - op so it doesn’t affect the app, but it is a nuisance and I feel like there should be a correct way I haven’t found

Expected Results

the listener is removed correctly

Actual Results

it isn’t and a No-Op react native warning is thrown due to react trying to update an unmounted component

Steps to Reproduce

add a listener to a result object in componentWillMount and remove it in componentWillUnmount

Code Sample

In the file where i define my realm instance I have the following:

const listeners = [];

export function addFineGrainListener(objectName, filter, callback) {
  const collection = realm.objects(objectName).filtered(filter);
  collection.addListener(callback);
  listeners.push({ objectName, filter, collection, callback });
}

export function removeFineGrainListener(objectName, filter) {
  const listenerObject = listeners.find(
    listener => listener.objectName === objectName && listener.filter === filter,
  );

  listenerObject.collection.removeListener(listenerObject.callback);
}

and my component has the following:

  componentWillMount() {
    addFineGrainListener(
      'JournalEntry',
      \`id ==[c] "${this.props.screenProps.noteId}"\`,
      this.updateUIDueToRealmChange,
    );
  }
  componentWillUnmount() {
    removeFineGrainListener(
      'JournalEntry',
      \`id ==[c] "${this.props.screenProps.noteId}"\`,
    );
  }

  updateUIDueToRealmChange = () => {
    this.forceUpdate();
  };

Version of Realm and Tooling

  • Realm JS SDK Version: 2.1.1
  • Node or React Native: RN 51.0
  • Client OS & Version: IOS 11.2
  • Which debugger for React Native: Default Chrome Debugger

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:3
  • Comments:32 (8 by maintainers)

github_iconTop GitHub Comments

5reactions
jmparsonscommented, Jan 14, 2018

removeAllListeners does nothing as well on collections.

3reactions
FlaviooLimacommented, Jan 29, 2018

@gunnigylfa Strange, the same happen to me, but it was my fault. This code of yours should work: dogs.removeAllLIsteners() can you do a test for me? Put the dog variavel initializeing globally like this:


var dogListnerGlobal;
export default class BlaBlaBlaComponent extends React.Component {
  constructor(props) {
    super(props);
    dogListnerGlobal = realm.objects('Dog');
    dogListnerGlobal.addListener(this.watchDogsCallBack);
  }

  watchDogsCallBack = () => {
    alert("something has change");
  }

  componentWillUnmount = () => {
    dogListnerGlobal.removeAllListeners();
  }

}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Can't remove event listener - Stack Overflow
As when I remove the event listener in the p function, it's getting removed without any error or bug. I am pretty sure...
Read more >
EventTarget.removeEventListener() - Web APIs | MDN
The removeEventListener() method of the EventTarget interface removes an event listener previously registered with EventTarget.
Read more >
Events | Maps JavaScript API - Google Developers
To remove all listeners for a specific event type for a specific instance, call clearListeners() , passing the instance name and the event...
Read more >
Create event listener bound to event source - MATLAB
Redefining or clearing the variable containing the handle of the listener (for example, el ) does not delete the listener. The event object...
Read more >
Remove an Event Handler - JavaScript Tutorial
element.removeEventListener(type, handler); · <button class="btn">Register</button> The following defines the click event handler: · function clickHandler(e) { ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found