Solved

problem with listener pattern using fast emulation

Posted on 2010-09-01
2
598 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of Google's most recent algorithm changes affecting local searches is entitled "The Pigeon Update." This update has dramatically enhanced search inquires for the keyword "Yelp." Google searches with the word "Yelp" included will now yield Yelp a…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

776 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