Remove listener - NFC plugin


#1

Hello, is there some good solution how to remove listener in NFC native plugin?

I saw that plugin source on github has nfc.removeNdefListener function but it is not expose on the Ionic layer therefore it is not possible to use it.

I tried add function removeNdefListener to @ionic-native/nfc index.js file but unfortunately it still doesn’t work.

this.nfc.addNdefListener(() => {
console.log(‘successfully attached ndef listener’);
}, (err) => {
console.log(‘error attaching ndef listener’, err);
}).subscribe((event) => {

});

I can not resolve my problem because each time I click on the button new listener is creating and call event function many times.
Thank you.


#2

The nfc plugin uses rxjs/Observable for the listeners, which has an unsubscribe function. See http://reactivex.io/rxjs/manual/overview.html#disposing-observable-executions

Just write:

this.myListener = this.nfc.addNdefListener(() => {
console.log(‘successfully attached ndef listener’);
}, (err) => {
console.log(‘error attaching ndef listener’, err);
}).subscribe((event) => {
…
});

//remove listener
this.myListener.unsubscribe();