<__nscfset :0x74957b0>在枚举时发生突变
我无法理解为什么我的代码崩溃了:
<__NSCFSet:0x74957b0>枚举时发生突变
我读过类似的主题,但他们的问题是代码在不同的线程中调用。我的代码总是在线程 1 中调用。
它有时会崩溃。
这是发生这种情况的代码:
- (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];
}
}
}
}
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];
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您收到的错误是由于您在循环遍历“观察者”集时更改了“观察者”集而引起的。
仅从您发布的片段中很难判断这可能是什么。您在 someObserver 上调用的委托中是否有某些内容更改了“观察者”集?
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?
您可以尝试简单地复制
observers
集并循环遍历该副本:如果您使用ARC(自动引用计数),则不需要使用
autorelease
。You could try simply copying the
observers
set and looping over the copy:If you're using ARC (automatic reference counting), you don't need to use
autorelease
.