Listener called multiple times with a single call

Hello,

I am using Ionic Native and Capacitor to create an ios app using a third-party swift framework.

I have multiple listeners active on my application as shown in the code below.

@objc func startScan(_ call: CAPPluginCall) {
let filter = call.getBool(“filter”) ?? true
self.notifyListeners(“didScan”, data: [“scanning”: true])
call.success()
}

Whenever I call a function like startScan, I do receive the didScan Listener but more than once.

<IonButton onClick={() => BandNativePlugin.startScan({filter: true})} >Start Scan

:zap: [log] - [TSX] didScan {“scanning”:true}
:zap: [log] - [TSX] didScan {“scanning”:true}

As I keep on calling different functions with different listeners, it keeps adding the numbers of calls returned for the listener. It seems like the listener is being called more than once but that is not the case (Since I am calling the notifyListener only once on the ios side).

:zap: [log] - [TSX] didScan {“scanning”:true}

:zap: [log] - [TSX] didScan {“scanning”:true}

:zap: [log] - [TSX] didScan {“scanning”:false}

:zap: [log] - [TSX] didScan {“scanning”:false}

:zap: [log] - [TSX] didSend {“function”:“Get Settings”,“params”:“KCTOldBLETaskBase: Failed to convert the model to JSON”}

:zap: [log] - [TSX] didSend {“function”:“Get Settings”,“params”:“KCTOldBLETaskBase: Failed to convert the model to JSON”}

:zap: [log] - [TSX] didSend {“function”:“Get Settings”,“params”:“KCTOldBLETaskBase: Failed to convert the model to JSON”}

:zap: [log] - [TSX] didReceive {“function”:“Get Settings”,“result”:“”,“error”:“errType=KCTErrorType,errInfo”}

:zap: [log] - [TSX] didReceive {“function”:“Get Settings”,“result”:“”,“error”:“errType=KCTErrorType,errInfo”}

:zap: [log] - [TSX] didReceive {“function”:“Get Settings”,“result”:“”,“error”:“errType=KCTErrorType,errInfo”}

Can someone please help me on this issue and explain me why I am getting multiple instances of same listener and why it is increasing?

I had the same issue and solved it by making sure to remove the listener inside ngOnDestroy or ionViewWillLeave to prevent it from stacking each time the view is entered.

You’re right to suspect the front end: in Capacitor the usual culprit is registering the same listener multiple times across re-renders or navigation, so you see duplicates even though native fires once. Add each listener once (e.g., in a single useEffect/ngOnInit) and keep a handle to remove it—Scalability—As your business grows, so must your technology