I am trying to add a background threaded operation to a NSOperation queue and want to make it execute in a sequence, so I set setMaxConcurrentOperationCount to 1 but not able to achieve synchronous process.
I tried with below code,
NSOperationQueue *queue = [NSOperationQueue new];
[queue setMaxConcurrentOperationCount:1];
[queue addOperationWithBlock:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
for (uint i=0; i<=9999999; i++) {
NSLog(@"Loop A");
}
});
}];
[queue addOperationWithBlock:^{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^{
for (uint i=0; i<=9999999; i++) {
NSLog(@"Loop B");
}
});
}];
That Will Log like,
2016-01-04 17:25:41.861 TestOperation[582:111196] Loop B
2016-01-04 17:25:41.861 TestOperation[582:111194] Loop A
2016-01-04 17:25:41.864 TestOperation[582:111196] Loop B
2016-01-04 17:25:41.866 TestOperation[582:111194] Loop A
2016-01-04 17:25:41.867 TestOperation[582:111196] Loop B
2016-01-04 17:25:41.867 TestOperation[582:111194] Loop A
2016-01-04 17:25:41.868 TestOperation[582:111194] Loop A
2016-01-04 17:25:41.869 TestOperation[582:111194] Loop A
and want this operation to complete Loop A first and then Loop B.