Solved

problem with listener pattern using fast emulation

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

CocoaPods is the best way to manage library dependencies in iOS and OS X projects. By using cocoa pods there is no need of downloading the code from github and copying to your project. There are plenty of open source libraries now available with C…
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 and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

790 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