According to "AVCaptureOutput.h" the AVCaptureVideoDataOutputSampleBufferDelegate delegate is described like this.

If the queue is blocked when new frames are captured, those frames will be automatically dropped at a time determined by the value of the alwaysDiscardsLateVideoFrames property.

How can I implement a similar functionality? So I can discard new operations if the queue is blocked.
Is it the default behavior of a serial queue?

有帮助吗?

解决方案

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--;
            });
        }
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top