Futurescale, Inc. PureMVC Home

The PureMVC Framework Code at the Speed of Thought


Over 10 years of community discussion and knowledge are maintained here as a read-only archive.

New discussions should be taken up in issues on the appropriate projects at https://github.com/PureMVC

Show Posts

* | |

  Show Posts
Pages: [1]
1  PureMVC Manifold / Bug Report / [ ANTIPATTERN ] Possible Problem with removing Mediators on: February 11, 2008, 08:57:00
Hi there Cliff,

first of all thanks a lot for sharing your great framework with us.

I've recently come across a problem I wanted to let you know about. Possibly it won't occur if one doesn't violate best practices advice, I'm not good enough a programmer to tell yet ;).

Here's what I've come across. If a notification is sent that a few observers are listening for, and this notification involves removing mediators which are actually also listening for that same notification it can happen that some of the observers won't hear the notification.

Consider this function in org.puremvc.core.view.View
:
public function notifyObservers( notification:INotification ) : void
{
if( observerMap[ notification.getName() ] != null ) {
var observers:Array = observerMap[ notification.getName() ] as Array;
for (var i:Number = 0; i < observers.length; i++) {
var observer:IObserver = observers[ i ] as IObserver;
observer.notifyObserver( notification );
}
}
}

One by one all observers are notified. If, say, there are 6 observers listening, and the 3rd observer responds to this notification sent, by removing the 2nd observer, the array will be shortened by one observer while the for-loop is still running. So all the indizes after the 2nd observer will be decreased by one thus causing chaos in the array. In this example the observer at the 4th position in the array will be moved to the 3rd position and miss the notification.

I hope I made myself clear, haven't had time to provide any samplecode up to now, but can if what I'm saying is not understood. My work-around is to make an exact copy of the observer array before entering the for-loop and using that. That way I can be sure that no observer will be missed. On the other hand removed observers will still be getting this one last notification which they actually shouldn't.

Thanks again for your great work, hopefully I've helped somehow to improve it.
Pages: [1]