iphone - Setting concurrent operation for NSOperationQueue causes only that number of operation -
myi have nsoperationqueue nsoperation objects in it
nsoperationqueue *aqueue = [[ nsoperationqueue alloc ] init]; [aqueue setmaxconcurrentoperationcount:3]; (int index=0; index<=5; index++) { myoperation *anoperation = [[myoperation alloc] init];//myoperation subclass nsoperation [aqueue addoperation:anoperation]; } nslog(@"number of operations:%d",[aqueue operationcount]);//it gives 5 count
the queue allows executes 3 operation @ time(as per definition). when try add 4th operation, adds queue, operation never executed , discarded.
ques: why queue discards operation more concurrence values?
nsoperationqueue
manages thread execute submitted operations on background. (since 10.6 using grand central dispatch). submitted operations executed on secondary thread default.
querying operation queue after submitting batch of operations - @ point queue might not have started execute operations , therefore correctly reports total operation count of 6.
if add delay before querying queue, might have finished operations , report count of 0.
sample code:
nsoperationqueue *aqueue = [[ nsoperationqueue alloc ] init]; [aqueue setmaxconcurrentoperationcount:3]; (int index=0; index<=5; index++) { myoperation *anoperation = [[myoperation alloc] init];//myoperation subclass nsoperation [aqueue addoperation:anoperation]; } double delayinseconds = 2.0; dispatch_time_t poptime = dispatch_time(dispatch_time_now, (int64_t)(delayinseconds * nsec_per_sec)); dispatch_after(poptime, dispatch_get_main_queue(), ^(void){ nslog(@"number of operations:%d",[aqueue operationcount]);//it gives 5 count });
Comments
Post a Comment