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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T11:40:54+00:00 2026-06-03T11:40:54+00:00

I met the exception called ‘Collection was mutated while being enumerated’ yesterday and saw

  • 0

I met the exception called ‘Collection was mutated while being enumerated’ yesterday and saw some solutions.I’ve got another idea.If I set an argument called ‘lock’ and each time I enumerate I lock it then unlock it after enumeration.Every time I’ll operate it,like enumerating or add object, I lock it first then unlock it after the operation.
Could someone tell me if it will work?

  • 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-03T11:40:55+00:00Added an answer on June 3, 2026 at 11:40 am

    I solved it.One of the problem is about thread safety.Use

        @synchronized (yourobject)
        {
        //do something with your object
        }
    

    that will work.It means that it’ll lock ‘yourobject’ so that other thread can’t modify it.Then unlock it after the ‘}’

    The other is some code about deleting objects in array.If you delete instantly,you’ll meet the exception called ‘Collection was mutated while being enumerated’.Create a temp array to contain objects you want to delete,then delete them all after the for loop.

     NSMutableArray *toDestroy = [NSMutableArray arrayWithCapacity:_contactor.count];
        for(Man *sprite in self.contactor)
        {
            if (sprite.hp<=0 && sprite.stat == stat_attack)
            {
                [sprite stopAllActions];
                [sprite animDead];
                [toDestroy addObject:sprite];
            }
        }
        [self.contactor removeObjectsInArray:toDestroy];
    

    instead of

    for(Man *sprite in self.contactor)
    {
        if (sprite.hp<=0 && sprite.stat == stat_attack)
        {
            [sprite stopAllActions];
            [sprite animDead];
            [self.contactor removeObject:sprite];
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I met KeyNotFound exception when i've tried to query the user list in rally
I have some MET data I want to validate which would look something like
I am developing WP7 app. I met some unexpected behavior. I use PerformanceProgressBar from
Well, when I'm trying to use 'inclusion' in Django, I met some confused problems
I met some compilation error but do not know what the problem is. The
I met exception when using annotated joined subclass, i don't know how to correct
Have you guys met something similar? Exception type: System.ComponentModel.Win32Exception Exception message: The operation completed
Recently I met the following exception at C# solution: Error 2 Could not load
I had the following code: Contract.Requires(somecondition, some message/*some comment*/); and while debugging at some
I met a strange problem. If I use for loop to render html into

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.