Вопрос

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