I have a number of ViewMediators which listen for a LOGOUT notification and remove themselves by using facade.removeMediator. Only half of them are removed. I traced the problem to notifyObservers in View.as. The for loop increments as the observers array decrements, which means you skip every other observer. My solution, after a bit of head scratching, is very simple: reverse the for loop. This fixed it for me:
public function notifyObservers( notification:INotification ) : void { if( observerMap[ notification.getName() ] != null ) { var observers:Array = observerMap[ notification.getName() ] as Array; // BAD: because observers that remove themselves decrement the array while i increments // for (var i:Number = 0; i < observers.length; i++) { // GOOD: for (var i:Number = observers.length - 1; i >= 0; i--) { var observer:IObserver = observers[ i ] as IObserver; observer.notifyObserver( notification ); } } }
I hope others find this helpful. I would suggest adding this fix to the next release.
Jay
|