Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1029797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:36:04+00:00 2026-05-16T12:36:04+00:00

Hey, right now I have implemented my own listener pattern. I will send an

  • 0

Hey, right now I have implemented my own listener pattern.
I will send an update to the listeners using fast enumeration.
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 enumeration 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:36:05+00:00Added an answer on May 16, 2026 at 12:36 pm

    It sounds like what you have now is the listener itself deciding whether it should be removed, and removing itself. That’s problematic because (a) as you say, it breaks your enumeration, but (b) because it’s a tricky abstraction– if the object that runs “update” doesn’t also control ownership in the listener list directly, your design pattern might run into problems anyways. I might suggest that you redefine update listeners like this:

    - (BOOL)update
    

    and return a BOOL indicating whether the listener should be removed (or kept, depending on your semantics). Then you could write the loop like this:

    NSMutableSet * listenersToBeRemoved = [NSMutableSet set];
    for (id<AProtocol> listener in _listeners) {
        BOOL shouldRemove = [listener update];
        if (shouldRemove) { 
            [listenersToBeRemoved addObject:listener];
        }
    }
    // Do this if _listeners is a Set, or whatever the equivalent is.
    [_listeners minusSet:listenersToBeRemoved];
    

    As others have suggested, if you do want to allow the listeners to remove themselves during the update process, it’s simple enough to just iterate through a local copy of the collection, instead of the collection itself. The syntax for that depends on whether _listeners is an array, a set, or something else, but see other answers or the docs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hey right now I'm using jQuery and I have some global variables to hold
Hey, I right now have a list of a struct that I made, I
Hey I have this code right here: http://pastie.org/534470 And on line 109 I get
I'm finishing up my first Codeigniter app and I have a question. Right now
I have a database of phrases that users will search for from their own
Hey there! I'm doing this project and right now I'm trying to: create some
I'm starting to get into PigLatin and I have a question... Right now I'm
I have just implemented my first Restlet application (finally :]) and now I'm onto
Hey guys, right now I'm trying to make a music discovery website just to
Hey, I've got a bit of a problem right now trying to figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.