I have a strange error which pop up recently. The NSOperationQueue says it has 1 object in it however I cannot access the NSOperation object inside it.
if ([[queue operations] count] > 0)
op = [queue.operations objectAtIndex:0];
But for some reason it ends up in the following exception: index 0 beyond bounds for empty array’
I understand the error message however I am surprised since I was checking the queue count before asking for the object itself.
Any ideas please?
Remember that operations are able to run on separate threads and usually are. An
NSOperationQueueactually has its own method for getting the count calledoperationCountand provides this word of caution:What you are running into is probably a concurrency issue. One thing to consider is to copy the operations array.
Update:
Here is an example of why you may see this happening very often