Serge
Serge

Reputation: 2129

<__NSCFSet: 0x74957b0> was mutated while being enumerated

I can't undarstand why my code is crashes:

<__NSCFSet: 0x74957b0> was mutated while being enumerated

Previously I read simular topics but their problem was that the code calls in different threads. My code always calls in Thread 1.

It crashes times to time.

Here is a code where it happens:

- (void)processReceivedResponse:(JTResponse *)aResponse {

    NSParameterAssert(aResponse);

    id <JTRequestDelegate> delegate = [self processResponseWithReceiver:aResponse];

    if (delegate == nil) {

        for (JTObserver *someObserver in observers) {

            if (someObserver.requestType == aResponse.type && 
                     ![someObserver.delegate isEqual:delegate]) {

                [someObserver.delegate didReceiveResponse:aResponse];
            }
        }
    }
}

Upvotes: 1

Views: 1591

Answers (2)

rob mayoff
rob mayoff

Reputation: 385580

You could try simply copying the observers set and looping over the copy:

for (JTObserver *someObserver in [[observers copy] autorelease]) {
    ...
}

If you're using ARC (automatic reference counting), you don't need to use autorelease.

Upvotes: 3

triangle_man
triangle_man

Reputation: 1092

The error you're getting is caused by something changing the "observers" set while you're looping through it.

It's hard to tell what that might be from just the snippet you posted. Is something in the delegate you're invoking on someObserver changing the "observers" set?

Upvotes: 3

Related Questions