12 Replies Latest reply on Sep 29, 2009 11:02 AM by Flex harUI

    event listener not being removed

    OzzyOGRicca Level 1

      I'm writing a scheduling program, and I have an HTTPService object called 'srv'. At the beginning of the program it sets srv.addEventListener(ResultEvent.RESULT, addToGrid); and calls srv.send();. Now in 'addToGrid' I have:

       

      srv.removeEventListener(ResultEvent.RESULT, addToGrid);
      srv.addEventListener(ResultEvent.RESULT, updateGrid);

       

      but it still calls 'addToGrid' after I call 'srv.send();' in another part of the program. It does play the new 'updateGrid' as well. Any idea why it still plays the 'addToGrid'?

        • 1. Re: event listener not being removed
          OzzyOGRicca Level 1

          Nobody's even got a guess? C'mon...

          • 2. Re: event listener not being removed
            Gregory Lafrance Level 6

            In this sample application I am not seeing the problem. How does your situation differ?

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
              creationComplete="init();">
              <mx:Script>
                <![CDATA[
                  import mx.rpc.events.ResultEvent;
                  import mx.collections.XMLListCollection;
                  [Bindable] private var xlc:XMLListCollection;

                  private function init():void{
                    srv.addEventListener(ResultEvent.RESULT, addToGrid);
                    srv.send();
                  }
                 
                  private function addToGrid(evt:ResultEvent):void{
                    trace("in addToGrid()");
                    var xmllist:XMLList = evt.result..item;
                    xlc = new XMLListCollection(xmllist);
                    srv.url = "datanew.xml";
                    srv.removeEventListener(ResultEvent.RESULT, addToGrid);
                    srv.addEventListener(ResultEvent.RESULT, updateGrid);
                    srv.send();       
                  }

                  private function updateGrid(evt:ResultEvent):void{
                    trace("in updateGrid()");
                    var xmllist:XMLList = evt.result..item;
                    xlc = new XMLListCollection(xmllist);
                  }
                ]]>
              </mx:Script>
              <mx:HTTPService id="srv" url="data.xml" resultFormat="e4x"/> 
              <mx:ComboBox dataProvider="{xlc}" labelField="firstData"/>
            </mx:Application>

             

            -------------- data.xml ---------------

            <?xml version="1.0" encoding="utf-8"?>
            <mydata>
              <item>
                <firstData>apples</firstData>
                <secondData>oranges</secondData>
              </item>
              <item>
                <firstData>beef</firstData>
                <secondData>chicken</secondData>
              </item>
              <item>
                <firstData>milk</firstData>
                <secondData>juice</secondData>
              </item>
              <item>
                <firstData>carrot</firstData>
                <secondData>tomato</secondData>
              </item>
            </mydata>


            -------------- datanew.xml ---------------

            <?xml version="1.0" encoding="utf-8"?>
            <mydata>
              <item>
                <firstData>new apples</firstData>
                <secondData>new oranges</secondData>
              </item>
              <item>
                <firstData>new beef</firstData>
                <secondData>new chicken</secondData>
              </item>
              <item>
                <firstData>new milk</firstData>
                <secondData>new juice</secondData>
              </item>
              <item>
                <firstData>new carrot</firstData>
                <secondData>new tomato</secondData>
              </item>
            </mydata>

            • 3. Re: event listener not being removed
              OzzyOGRicca Level 1

              No, its pretty much the same except that the second 'srv.send();' is called from another component which references this object. In fact the two functions (addToGrid, updateGrid) are very similar except that addtoGrid initializes a lot of things that updateGrid is just supposed to update with the data retreived from 'srv.lastResult'. So the way things are going now it calls both functions and ends up adding extra components that shouldn't be there. I've tried using weak references, no dice; tried adding removeEventListener's both with and without using 'useCapture', no dice.

              • 4. Re: event listener not being removed
                OzzyOGRicca Level 1

                Anybody have any idea what might be causing this behavior?

                • 5. Re: event listener not being removed
                  Flex harUI Adobe Employee

                  Lots of possibilities:

                   

                  1) You have addToGrid methods in several context and are removing the wrong one

                  2) The code you use to addEventListener for addToGrid is getting called again

                  3) You aren't really adding addToGrid as the handler.  It is being called by the actual handler.

                   

                  Maybe post the relevant code snippets

                   

                  Alex Harui

                  Flex SDK Developer

                  Adobe Systems Inc.

                  Blog: http://blogs.adobe.com/aharui

                  • 6. Re: event listener not being removed
                    OzzyOGRicca Level 1

                    Hi Alex,

                     

                    1.) there is only one 'addToGrid' function in my entire program, and it's the one being called

                    2.) i checked for this, it is not being explicitly readded as a listener

                    3.) im not sure what you mean here.

                     

                    Here is a basic idea of my code:

                     

                    1.) in my Application:
                    private function initVars():void {
                    
                              schedule_XML_location = Application.application.parameters.xml_file;
                              .
                              .
                              .
                              srv.addEventListener(ResultEvent.RESULT, addToGrid);
                              srv.addEventListener(FaultEvent.FAULT, alertUserError);
                              .
                              .
                              .
                    
                              // Ok, we're ready to get the XML document with the relevant data
                              this.dispatchEvent(new ModeratorCalendarEvent(ModeratorCalendarEvent.BEGIN_REFRESH_APP_XML_INFO));
                              srv.send();
                    }
                    
                    2.)  in my Application:
                    private function addToGrid(e:ResultEvent):void {
                              // set up visual objects
                              .
                              .
                              .
                    
                              /**** EVENT LISTENERS ****/
                              .
                              .
                              .
                    
                              srv.removeEventListener(ResultEvent.RESULT, addToGrid);
                              srv.addEventListener(ResultEvent.RESULT, updateGrid);
                    }
                    
                    3.) in another file, which calls srv.send(); after
                    'schedule_XML_location' is updated (its just a string that holds a
                    URL).
                    4.)
                    private function updateGrid(r:ResultEvent):void {
                    // does similar functionality to addToGrid
                    
                    }
                    
                    • 7. Re: event listener not being removed
                      Flex harUI Adobe Employee

                      For #3, I would set a breakpoint in addToGrid and check the callstack to make sure that some other function didn't call it.

                       

                      Have you verified the order of the calls?  Maybe two sends are out there and come back in a different order than you expected.

                       

                       

                      Alex Harui

                      Flex SDK Developer

                      Adobe Systems Inc.

                      Blog: http://blogs.adobe.com/aharui

                      • 8. Re: event listener not being removed
                        OzzyOGRicca Level 1

                        Here's the stack trace:

                         

                        Main Thread (Suspended)   
                            Eclient_moderator_calendar/addToGrid   
                            flash.events::EventDispatcher/dispatchEventFunction [no source]   
                            flash.events::EventDispatcher/dispatchEvent [no source]   
                            HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent   
                            mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler   
                            mx.rpc::Responder/result   
                            mx.rpc::AsyncRequest/acknowledge   
                            DirectHTTPMessageResponder/completeHandler   
                            flash.events::EventDispatcher/dispatchEventFunction [no source]   
                            flash.events::EventDispatcher/dispatchEvent [no source]   
                            flash.net::URLLoader/onComplete [no source]  

                        • 9. Re: event listener not being removed
                          OzzyOGRicca Level 1

                          more relevant info: when ...datasets.hasOwnProperty("updated_xml_file"), every one of those checks will return false, but even after doing the 'par_app.srv.send();', it still calls addToGrid. I even tested it by telling it not to use 'updateGrid' at all, so these checks are returning correctly.

                           

                          private function triggerCascadeUpdate(r:ResultEvent):void {
                                      var par_app:Eclient_moderator_calendar = this.parentApplication as Eclient_moderator_calendar;
                                      if (http_service_object_send_copy_interview_info.lastResult != null &&
                                          http_service_object_send_copy_interview_info.lastResult.hasOwnProperty('datasets') &&
                                          http_service_object_send_copy_interview_info.lastResult.datasets != null) {
                                              
                                          if (http_service_object_send_copy_interview_info.lastResult.datasets.hasOwnProperty("updated_xml_file")) {
                                              // Success!
                                              this.dispatchEvent(new ModeratorCalendarEvent(ModeratorCalendarEvent.BEGIN_REFRESH_APP_XML_INFO)); // pops up a progress bar
                                              par_app.schedule_XML_location = http_service_object_send_copy_interview_info.lastResult.datasets.updated_xml_file;
                                              InterviewScheduleAlert.show("srv.willTrigger addToGrid: " + 
                                                                          par_app.srv.willTrigger(ResultEvent.RESULT) + 
                                                                          "\nsrv.hasEventListener addToGrid: " + 
                                                                          par_app.srv.hasEventListener(ResultEvent.RESULT) + 
                                                                          "\nparentApplication.hasEventListener addToGrid: " + this.parentApplication.willTrigger(ResultEvent.RESULT));
                                              
                                              par_app.srv.send();
                                          }
                                          else {
                                              ...
                                          }
                          .
                          .
                          .
                          }
                          
                          • 10. Re: event listener not being removed
                            Flex harUI Adobe Employee

                            Both calls to addToGrid have the same callstack?

                             

                            It is probably time to try to create the simplest reproduction test case.

                             

                            Alex Harui

                            Flex SDK Developer

                            Adobe Systems Inc.

                            Blog: http://blogs.adobe.com/aharui

                            • 11. Re: event listener not being removed
                              OzzyOGRicca Level 1

                              first run:

                              Main Thread (Suspended)   
                                  Eclient_moderator_calendar/addToGrid   
                                  flash.events::EventDispatcher/dispatchEventFunction [no source]   
                                  flash.events::EventDispatcher/dispatchEvent [no source]   
                                  HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent   
                                  mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler   
                                  mx.rpc::Responder/result   
                                  mx.rpc::AsyncRequest/acknowledge   
                                  DirectHTTPMessageResponder/completeHandler   
                                  flash.events::EventDispatcher/dispatchEventFunction [no source]   
                                  flash.events::EventDispatcher/dispatchEvent [no source]   
                                  flash.net::URLLoader/onComplete [no source] 

                               

                              second run:

                              Main Thread (Suspended)   
                                  Eclient_moderator_calendar/addToGrid   
                                  flash.events::EventDispatcher/dispatchEventFunction [no source]   
                                  flash.events::EventDispatcher/dispatchEvent [no source]   
                                  HTTPOperation/http://www.adobe.com/2006/flex/mx/internal::dispatchRpcEvent  
                                  mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal::resultHandler  
                                  mx.rpc::Responder/result   
                                  mx.rpc::AsyncRequest/acknowledge   
                                  DirectHTTPMessageResponder/completeHandler   
                                  flash.events::EventDispatcher/dispatchEventFunction [no source]   
                                  flash.events::EventDispatcher/dispatchEvent [no source]   
                                  flash.net::URLLoader/onComplete [no source] 

                              • 12. Re: event listener not being removed
                                Flex harUI Adobe Employee

                                Definitely time to make a reduced test case.  Strip it down to fewest lines required to reproduce the problem

                                 

                                Alex Harui

                                Flex SDK Developer

                                Adobe Systems Inc.

                                Blog: http://blogs.adobe.com/aharui