Link to home
Start Free TrialLog in
Avatar of privateson
privateson

asked on

problem with listener pattern using fast emulation

Hey, right now I have implemented my own listener patten.
I will send an update to the listeners using fast emulation.
the code will look like this

- (void) updateListeners {
for (id<AProtocol>listener in _listeners)
{
   [listener update];
}

and in listener, i implement method for AProtocol, which is update.
suppose there are n object in _listeners, and m number of listener such that  m < n want to remove it self from listen when listener's update method is called.
The problem with this is that I can't remove when the fast emulation is ongoing, I will get an error.
In order to make the listener more dynamic so that we can remove listener from _listeners when update method is called, what would be the solution?( I don't want to use NSNotificationCenter)
Avatar of pgnatyuk
pgnatyuk
Flag of Israel image

>>fast emulation
Fast Enumeration - that's the feature name.
http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/ObjectiveC/Articles/ocFastEnumeration.html

"Enumeration is “safe”—the enumerator has a mutation guard so that if you attempt to modify the collection during enumeration, an exception is raised."
"Since mutation of the object during iteration is forbidden, you can perform multiple enumerations concurrently."

So do not remove your listeners from such loop. If you want to delete a listener, use a standard C loop. Or do not remove the listeners at all, but add an attribute "Active", etc.


 
ASKER CERTIFIED SOLUTION
Avatar of kouddy
kouddy

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial