Race condition firing events in AS3

∥☆過路亽.° 提交于 2019-12-08 04:55:46

问题


I have some troubles firing and removing events in the right chronicle order. The code below gives the following output:

  • save poster into db, and dispatch event
  • calling service, dispatch event removed = false
  • calling service, dispatch event removed = false
  • calling service, dispatch event removed = true
  • save poster into db, and dispatch event
  • save poster into db, and dispatch event

of course this should be more something like:

  • save poster into db, and dispatch event
  • calling service, dispatch event removed = true
  • save poster into db, and dispatch event
  • calling service, dispatch event removed = true
  • save poster into db, and dispatch event
  • calling service, dispatch event removed = true

Can someone help me with this? I'm running out of ideas on how to tackle this.

thx!

    for(var i:int = 0;i< 3;i++){
        createPoster();         
    }

    function createPoster(){
        Main.db.savePoster();
        Main.db.addEventListener(Config.evt_SAVEPOSTER_READY, callService);
    }

    function callService(){
       Main.db.removeEventListener(Config.evt_SAVEPOSTER_READY, callService);
    }

回答1:


The problem is that you are registering same function callService for same event Config.evt_SAVEPOSTER_READY on single EvenDispatcher objectdb. So as soon first savePoster dispatches the event after successfully saving the poster, db receives the event and three eventHandlers (in this case callService) are called because callService is registered thrice. So one solution would be dispatching the events from Poster.

for(var i:int = 0;i< 3;i++){
  createPoster();
}
function createPoster(){
  poster = Main.db.savePoster();
  poster.addEventListener(Config.evt_SAVEPOSTER_READY, callService);
}
function callService(e:PosterEvent){
  e.target.removeEventListener(Config.evt_SAVEPOSTER_READY, callService);
}



回答2:


Have you checked what happens when you loop through one item only? Seems to me you are not queuing your routines properly.

You might probably want to add an event listener to your Main.db object only once and remove it when you have gotten all your 'posters' successfully saved.




回答3:


Is the db call (Main.db.savePoster();) synchronous - does it return only after the action is completed? Since you are calling addEventListener after the db call, the event listener (for the first iteration at least) will not be called if the db-call is synchronous.

Is the Main.db the same instance in all the three iterations? If it is, you don't have to register the same event listener thrice for it - once would be enough. Call addEventListener before starting the for-loop. Keep a counter for tracking the number of calls to the callService and call removeEventListener once the counter hits loop count (3, in this case).



来源:https://stackoverflow.com/questions/1834525/race-condition-firing-events-in-as3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!