This "don't dispatch new block if the queue still running previous block" is definitely not the default behavior. If you want to do that, you can write your own dispatch routine that checks to see if there are operations running before adding a new one.
If using NSOperationQueue
, you can leverage the existing operationCount
property.
- (void)addOperationIfQueueEmptyWithBlock:(void (^)(void))block
{
@synchronized (self) {
if (self.queue.operationCount == 0)
[self.queue addOperationWithBlock:block];
}
}
If using GCD, you'll just maintain your own count property:
@property (atomic) NSInteger operationCount;
And then:
- (void)dispatchAsyncTaskIfQueueEmpty:(void (^)(void))block
{
@synchronized (self) {
if (self.operationCount == 0) {
self.operationCount++;
dispatch_async(self.queue, ^{
block();
self.operationCount--;
});
}
}
}