iphone - NSOperationQueue out of range exception -
i have strange error pop recently. nsoperationqueue says has 1 object in cannot access nsoperation object inside it.
if ([[queue operations] count] > 0) op = [queue.operations objectatindex:0];
but reason ends in following exception: index 0 beyond bounds empty array'
i understand error message surprised since checking queue count before asking object itself.
any ideas please?
remember operations able run on separate threads , are. nsoperationqueue
has own method getting count called operationcount
, provides word of caution:
the value returned method reflects instantaneous number of objects in queue , changes operations completed. result, time use returned value, actual number of operations may different. should therefore use value approximate guidance , should not rely on object enumerations or other precise calculations.
what running concurrency issue. 1 thing consider copy operations array.
nsarray *ops = [queue.operations copy]; if ([ops count] > 0) { op = [ops objectatindex:0]; //you can check if has finished using [op isfinished]; //and need here } [ops release];
update:
here example of why may see happening often
//set , start nsoperation ... //here call operations put sort of lock //around operations retrieve them operation //finished , waiting lock complete remove //the operation. operations call returns copy. if([[que operations] count] > 0) { //now operation queue can access operations , remove //the item lock released (it can access //as before call , count) //uh oh there 0 operations op = [queue.operations objectatindex:0]; }
Comments
Post a Comment