Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

Ti.App.addEventListener - Zombies!

Ahhhh, I got some undead eventlisteners in my app!

I very often open subwindow A which communicates itself with own detail-sub-windows B, C & D via Ti.App.addEventListener('myListener',{ Ti.API.info("it’s me!"); });. I need to instantiate the event within window A because it manipulates table-stuff there.

Now I want to remove that global event listener once my window A closes, but somehow the line Ti.App.removeEventListener('myListener',{}); does not work. Because when I fire that event again MANY event listener instances reply. So they are still alive!

What’s the best practice for the "killing" of global event listener instances?

— asked August 31st 2010 by Sebastian
  • eventlistener
0 Comments

5 Answers

  • I wrote about a solution here: http://petesaia.com/2012/10/appcelerator-event-garbage-collection/

    var globals = {};
    globals.EventGarbageCollection = function ( context ) {
        var instance = this;
        this.collection = [];
        this.context = context;
        this.context.addEventListener('close', function ( ) {
            instance.empty();
        });
    };
    globals.EventGarbageCollection.prototype = {
        collect : function ( evt, method ) {
            this.collection.push([evt, method]);
        },
        addEvent : function ( evt, method ) {
            Ti.API.addEventListener(evt, method);
            this.collect(evt, method);
        },
        empty : function ( ) {
            for ( var i = 0, len = this.collection.length; i < len; i++ ) {
                Ti.API.removeEventListener(this.collection[i][0], this.collection[i][1]);
            }
            this.collection = [];
        }
    };
    
    — answered October 2nd 2012 by Pete Saia
    permalink
    2 Comments
    • Good solution

      — commented October 5th 2012 by Thai Bui
    • Pete thx for this code. Just three clarification questions. (1) Which of these two codes (this one) or the one on the gist hub do you recommend for use? (2) Not sure how to apply either code to component level listeners e.g. all the button click events that open and close windows in an app. I have 9 such buttons with click event listeners . (3) What is the usage of the code in this thread? (4) The other code on the gist showed a window creation without window properties. Can I redefine that line of code in the usual way of creating window with properties and omit the (args) i.e. write Ti.Ui.createWindow({})?

      Thx for your response

      — commented December 27th 2012 by adebisi oladipupo
  • I found the solution here (the starred one):
    http://developer.appcelerator.com/question/33551/removeeventlistener-not-working

    — answered August 31st 2010 by Sebastian
    permalink
    0 Comments
  • Here comes an example for my solution. For me it works under 1.4.1 - you don’t need to assign a variable to the event listener. Just add a close-handler to your window where you assigned the Ti.App.eventListener and remove it there again. It even removes multiple instances/threads(?) of one window:

    
    function doit(e)
    {
       //do something here
    }
    
    Ti.App.addEventListener('doit', doit);
    
    win.addEventListener('close', function()
    {
        Ti.App.removeEventListener('doit',doit);
    });
    
    — answered September 13th 2010 by Sebastian
    permalink
    0 Comments
  • Hi, same issue here. The solution doesn't work with Ti.App.addEventListener. I am encountering this issue only on sdk 1.4.1. It works fine in 1.4.0 even without assigning the eventlisteners to a variable.

    Anyone having same issue resolved?

    Thanks,

    — answered September 11th 2010 by Sravan Vinjamuri
    permalink
    0 Comments
  • Contexts are broken to a certain extent (as of titanium 3.0.2) in that they block Ti.App events. Here is a reference to my answer to a similar question about how to get "real" multi-threading in titanium mobile here

    — answered March 4th 2013 by simon giles
    permalink
    0 Comments
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.