I got -[__NSOrderedSetM removeObjectAtIndex:]: index 0 beyond bounds for empty ordered set
in the following code:
- (void)moveAdapter:(SUBaseAdapter *)adapter
from:(NSMutableOrderedSet *)src
to:(NSMutableOrderedSet *)trg {
SS_ASSERT(adapter);
SS_ASSERT(src);
SS_ASSERT(trg);
dispatch_barrier_sync(self.adaptersQueue, ^{ // CONCURRENT QUEUE
if (![src containsObject:adapter]) {
NSString *message = [NSString stringWithFormat:@"moveAdapter: %@ not in src", adapter.providerName];
[[SULoggerManager sharedInstance] log:message level:SU_LOG_INTERNAL tag:TAG_INTERNAL];
return;
}
if ([trg containsObject:adapter]) {
NSString *message = [NSString stringWithFormat:@"moveAdapter: %@ already in trg", adapter.providerName];
[[SULoggerManager sharedInstance] log:message level:SU_LOG_INTERNAL tag:TAG_INTERNAL];
return;
}
[trg addObject:adapter];
[trg sortUsingDescriptors:@[self.sortDescriptor]];
[src removeObject:adapter]; //CRASH HERE
});
}
So, I'm not explicitly calling removeObjectAtIndex:
, but maybe removeObject:adapter
does?
Here's what I don't understand:
- I tried adding
anObject
to a set, then calledremovedObject:anObject
twice, had no problem. - I thought maybe it's a concurrency issue, where two different threads enter
removeObject
thinking it's in position 1, and then the second thread fails, but I am usingdispatch_barrier_sync
, and I don't remove objects from 'src' anywhere else.
Any hints?