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 7843323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T16:31:21+00:00 2026-06-02T16:31:21+00:00

I have to delete an object of an array every now and then, when

  • 0

I have to delete an object of an array every now and then, when I do it I get this error.

Collection < CALayerArray: 0xc4f3b20> was mutated while being enumerated

The error appears on this method, which is the accesor of the Array:

- (NSArray *)occupantsArray
{
    if (dispatch_get_current_queue() == moduleQueue)
    {
        return occupantsArray;
    }
    else
    {
        __block NSArray *result;

        dispatch_sync(moduleQueue, ^{ //ON THIS LINE
            result = [occupantsArray copy];
        });

        return [result autorelease];
    }
}

As you can see Im taking care of not returning the original array but a copy, but it still crashes.

Also the method where Im deleting elements of the array is this.

- (void)eraseJIDFromArray:(NSString*)jid{

    dispatch_block_t block = ^{
        NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
        int i = 0;
        for(NSString *jidAct in occupantsArray){
            if([jidAct isEqualToString:jid]){
                [occupantsArray removeObjectAtIndex:i];
            }
            i++;
        }
        [pool drain];
    };

    if (dispatch_get_current_queue() == moduleQueue)
        block();
    else
        dispatch_async(moduleQueue, block);



}

The array can have upto 200 elements, so it can take some time to go through all of them, but I’m setting queues, dont know what else I can do.

Any ideas?

Thanks.

  • 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-06-02T16:31:22+00:00Added an answer on June 2, 2026 at 4:31 pm

    This code:

    for(NSString *jidAct in occupantsArray){
        if([jidAct isEqualToString:jid]){
            [occupantsArray removeObjectAtIndex:i];
        }
        i++;
    }
    

    is probably causing your problems. You shouldn’t be removing elements while enumerating the array. The way you avoid this is by using an NSMutableIndexSet:

    NSMutableIndexSet *indexes = [NSMutableIndexSet set]; // assuming NSInteger i;
    for(NSString *jidAct in occupantsArray){
        if([jidAct isEqualToString:jid]){
            [indexes addIndex:i];
        }
        i++;
    }
    [occupantsArray removeObjectsAtIndexes:indexes];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array list containing objects from the class EventOnMap . Every object
I want to have a parent object delete itself and it's child object in
I have two tables without any cascade deleting. I want to delete parent object
I have an object that I'm freeing with delete, and it has a char*
I have to delete some elements of my array, but without rearrange array. If
i have this working code to delete files and folders from s3. how would
I have something like this var myArray = []; myArray.push(someObject); but then if I
I have a Ruby (1.9.2) array which I need to remove an object from.
I have a picture array that is stored globally. Every second I choose a
I have an array: array = [S1,S2,S3,S4_a,S4_b,S5_a,S5_b,S5_c etc....] How can I delete all the

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.