DispatchSerialQueue minimum OS support

Hi Team,

We intend to create a custom serial dispatch queue targetting a global queue.

let serialQueue = DispatchQueue(label: "corecomm.tallyworld.serial", target: DispatchQueue.global(qos: .default))

The documentation for DispatchQueue init does not show any minimum OS versions. BUT DispatchSerialQueue init does show iOS 17.0+ iPadOS 17.0+ Mac Catalyst macOS 14.0+ tvOS 17.0+ visionOS watchOS 10.0+.

Does that mean - I will not be able to create a custom serial dispatch queue below iOS 17?

Answered by DTS Engineer in 847526022
We intend to use it for our communication subsystem - to associate it and start [NWConnection]

OK, cool. Then I have some good news (-:

Network framework doesn’t require you to use a serial queue. The parameter for, say, connection.start(queue:) is of type DispatchQueue.

Now, using anything other than a serial queue is, of course, bananas. For a concrete example of where using a concurrent queue with Network framework caused problems, see this thread. However, the important point is that this desire to use a serial queue isn’t encoded in the type system. That is, you’re not required to use DispatchSerialQueue.

Given that, it’s fine to create a DispatchQueue for this work as long as the underlying queue is serial. So, for example, this code compiles:

let connection = NWConnection(host: "example", port: 443, using: .tls)
let queue = DispatchQueue(label: "network-queue", target: nil)
connection.start(queue: queue)

even with the deployment target set to macOS 10.14, that is, the OS release where Network framework was introduced.

And if you want to set a target queue for your serial queue, you can do that using the target parameter that I’ve left nil in this example.

ps Your example does this:

let serialQueue = DispatchQueue(…, target: DispatchQueue.global(qos: .default))

That’s weird because the default global concurrent queue is the default target for a serial queue. IMO you should only set a target if it’s something other than this default.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

This is tricky. Dispatch serial queues, the concept, have existed since the Dawn of Time™ [1]. However, DispatchSerialQueue, the Swift type, is only available on more recent systems. So, you can certainly create a Dispatch serial queue on older systems [2], but you may not be able to use it in all cases.

What are you planning to do with this serial queue?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Well, since the introduction of Dispatch back in macOS 10.6 / iOS 4 (-:

[2] Although if you want to target a queue, be aware that setting the target on create is done with dispatch_queue_create_with_target, which was introduced in macOS 10.12 / iOS 10. Alternatively, dispatch_set_target_queue has been around since the introduction of Dispatch.

Thanks for a prompt response @DTS Engineer.

We intend to use it for our communication subsystem - to associate it and start the NWConnections and NWListeners we will be creating - for async execution of various associated handlers. We don't intend to directly use the DispatchSerialQueue, the Swift type, but as DispatchQueue object.

We intend to use it for our communication subsystem - to associate it and start [NWConnection]

OK, cool. Then I have some good news (-:

Network framework doesn’t require you to use a serial queue. The parameter for, say, connection.start(queue:) is of type DispatchQueue.

Now, using anything other than a serial queue is, of course, bananas. For a concrete example of where using a concurrent queue with Network framework caused problems, see this thread. However, the important point is that this desire to use a serial queue isn’t encoded in the type system. That is, you’re not required to use DispatchSerialQueue.

Given that, it’s fine to create a DispatchQueue for this work as long as the underlying queue is serial. So, for example, this code compiles:

let connection = NWConnection(host: "example", port: 443, using: .tls)
let queue = DispatchQueue(label: "network-queue", target: nil)
connection.start(queue: queue)

even with the deployment target set to macOS 10.14, that is, the OS release where Network framework was introduced.

And if you want to set a target queue for your serial queue, you can do that using the target parameter that I’ve left nil in this example.

ps Your example does this:

let serialQueue = DispatchQueue(…, target: DispatchQueue.global(qos: .default))

That’s weird because the default global concurrent queue is the default target for a serial queue. IMO you should only set a target if it’s something other than this default.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

DispatchSerialQueue minimum OS support
 
 
Q