문제

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