AVCaptureSession and concurrency

AVFoundation isn’t really my area of expertise, but I have a couple of comments to share.

In Apple's camera example

Most of Apple’s sample code is still set to build in the Swift 5 language mode )-: If you’re trying to integrate code from such samples into a Swift 6 project, you are likely to run into problems like this.

No problem with DispatchQueue.global(qos: .background).async.

You really don’t want to be using Dispatch global queues for stuff like this. See Avoid Dispatch Global Concurrent Queues.

Reading the AVCaptureSession docs it’s clear that this type is deeply integrate with Dispatch queues. My standard approach for dealing with such APIs is to model the object as an actor, with a custom executor that’s bound to the same queue as the underlying object. The AVCam sample code seems to take that approach. I’ve not looked at it in depth, but it seems like something that’d be worthwhile for you to explore.

[Since writing the above, I discovered that Neph started a new thread about this: Switch to background thread, then back to main once it’s done]

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

1 Like