Titanium JIRA Archive
Appcelerator Community (AC)

[AC-6667] iOS: removeEventListener removes "any" callback

GitHub Issuen/a
TypeBug
Priorityn/a
StatusOpen
ResolutionUnresolved
Affected Version/sn/a
Fix Version/sn/a
ComponentsTitanium SDK & CLI
Labelsios
Reportermartin
AssigneeRene Pot
Created2021-01-26T15:00:01.000+0000
Updated2021-01-27T16:46:52.000+0000

Description

The removeEventListener should only remove a callback function that was previously added, but on iOS it removes any one with the same event type, see test case below.
const win=Ti.UI.createWindow({
	backgroundColor:'#fff'
});
const view=Ti.UI.createButton({
	title:'This button does nothing on OS...'
});
view.addEventListener('click',()=>{
	alert('This alert does not open on iOS...')
});	
view.removeEventListener('click',()=>{}); //This removes the callback added above, but it shouldn't!
win.add(view); 
win.open();

Comments

  1. Rene Pot 2021-01-27

    Hi, removeEventListener indeed removes events of the same type, otherwise, it would be impossible to remove anonymous callbacks. It is ALWAYS recommended to use named callbacks so you can prevent this yourself.
  2. martin 2021-01-27

    Hi Rene, Anonymous functions are not the point here. You could change my example to use named callbacks and it will give the same result:
       const win=Ti.UI.createWindow({
       	backgroundColor:'#fff'
       });
       const view=Ti.UI.createButton({
       	title:'This button does nothing on OS...'
       });
       view.addEventListener('click',callback1);	
       view.removeEventListener('click',callback2); //This removes the callback added above, but it shouldn't!
       win.add(view); 
       win.open();
       function callback1() {
       	alert('This alert does not open on iOS...')
       }
       function callback2() {	
       }
       
    If testing this on Android it will correctly show the alert when clicking the button. On iOS it will not.
  3. Rene Pot 2021-01-27

    Thanks for clarifying that, that wasn't clear from your original sample, there it actually behaved as expected. I will have a look at this one and get back to you!

JSON Source