brainforked
brainforked

Reputation: 139

GenericException : Collection was mutated while being enumerated

I have searched for this problem and I know it is caused because an array is manipulated while its enumeration. Earlier in my code, I was using :

-(BOOL)busy
{
for(DataRequest* request in self.active)
    if(!request.invisible)
        return YES;

return NO;
}

and -(BOOL)busy is being called very frequently as the data loads from the server. Also in my code I have some lines which adds and removes objects in self.active. And because of this I was getting the exception. Then I made a change in the code as:

-(BOOL)busy
{
self.tempActive = self.active;
for(DataRequest* request in _tempActive)
    if(!request.invisible)
        return YES;

return NO;
}

but I am still getting the same exception. What am I doing wrong, any ideas? This is the only code where self.tempActive is used.

The exception I am getting is:

*** Terminating app due to uncaught exception 'NSGenericException', reason: '*** Collection <__NSArrayM: 0x9ec8f60> was mutated while being enumerated.'

Upvotes: 0

Views: 930

Answers (1)

borrrden
borrrden

Reputation: 33421

You need to understand the difference between classes and stack objects (like ints and things). Setting something equal to something else does not make a copy of it if it is a class. You need to use [self.active copy] instead.

Upvotes: 3

Related Questions