Solved

problem with listener pattern using fast emulation

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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 recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

911 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

23 Experts available now in Live!

Get 1:1 Help Now