Solved

problem with listener pattern using fast emulation

Posted on 2010-09-01
2
595 Views
Last Modified: 2013-11-25
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)
0
Comment
Question by:privateson
2 Comments
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33579650
>>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.


 
0
 
LVL 2

Accepted Solution

by:
kouddy earned 500 total points
ID: 33601913
generally when you are doing fast emulation or even for loop, you should prevent removing listener from listener list.
You should remove it after you go through all the list.
NSIndexSet can be helpful in this case since it can hold number of index in listener.
after you finish emulating, then remove the items in listener using nsindexset
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This algorithm (in C#) will resize any image down to a given size while maintaining the original aspect ratio. The maximum width and max height are both optional but if neither are given, the original image is returned. This example is designed t…
Iteration: Iteration is repetition of a process. A student who goes to school repeats the process of going to school everyday until graduation. We go to grocery store at least once or twice a month to buy products. We repeat this process every mont…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now