An object that manages the serial or parallel execution of tasks in the main or background thread of your app.
Declaration
class DispatchQueue : DispatchObject
Dispatch queues are FIFO (first in, first out) queues that allow applications to pass tasks in the form of block objects. Dispatch queues perform tasks serially or in parallel. Tasks placed on the dispatch queue are executed in "any thread" managed by the system. ** The system does not guarantee "in which thread the task will be executed", except for the dispatch queue, which is the main thread of the app. ** **
Tasks are scheduled synchronously or asynchronously. When you schedule a task synchronously, the code waits for the next task until the item finishes executing. When you schedule work items asynchronously, other tasks continue to run while they are running elsewhere.
** A deadlock occurs when trying to perform work synchronously on the main queue. ** **
When designing tasks that run in parallel, avoid calling "methods that block running threads." When a "task scheduled by a parallel dispatch queue" blocks that thread, the system creates an additional thread to run "other parallel tasks in the queue." If too many tasks are blocked, the system can run out of threads in the app.
Creating a "private parallel dispatch queue" can also drain your app's threads. Each dispatch queue consumes thread resources, so creating additional parallel dispatch queues will consume more and more threads. Instead of creating a private parallel queue, submit the task to the "Parallel Global Dispatch Queue". For serial tasks, set the serial queue target to one of the "simultaneous global queues". By doing so, you can maintain the behavior of serial queues while minimizing "queues that create separate threads".
The dispatch queue associated with the main thread of the current process.
Declaration
class var main: DispatchQueue { get }
The system associates the automatically created main queue with the application's main thread. The app performs a "block sent to the main queue" in one of the following approaches.
--Call the dispatchMain ()
method
--Call the UIApplicationMain (_: _: _: _ :)
method on iOS and the NSApplicationMain (_: _ :)
method on macOS to start the app.
--Use the main thread CFRunLoop
Calling the suspend ()
, resume ()
, and dispatch_set_context (_: _ :)
methods on the queue in this property has no effect on the queue.
If the main thread does not respond for too long, you may get a 0x8badf00d
exception on the main thread of mach_msg_trap
. On iOS, this exception can occur if an app detects that it has failed to respond to a particular UI event in time. There is a guard dog on iOS that monitors the responsiveness of the UI.
If your app has tasks that take a long time to perform, such as network connectivity, run them in the system's global queue or another background dispatch queue. If possible, use asynchronous calls.
Returns a "system global queue" with a specified service quality level.
Declaration
class func global(qos: DispatchQoS.QoSClass = .default) -> DispatchQueue
This method returns the best queue to perform the task at the specified level of service quality. Calling the suspend ()
, resume ()
, and dispatch_set_context (_: _ :)
functions does not affect the returned queue.
Tasks submitted to the returned queue are scheduled in parallel relative to each other.
An example of creating a background queue with a standard service quality level
let backgroundQueue = DispatchQueue.global(qos: .default)
init(label:qos:attributes:autoreleaseFrequency:target:) Create a new dispatch queue that can send "blocks of code".
Declaration
convenience init(label: String,
qos: DispatchQoS = .unspecified,
attributes: DispatchQueue.Attributes = [],
autoreleaseFrequency: DispatchQueue.AutoreleaseFrequency = .inherit,
target: DispatchQueue? = nil)
The parameters are explained below.
label
A label that identifies the queue in debug tools such as crash reports. Applications, libraries, and frameworks can all create their own dispatch queues, so we recommend the reverse DNS naming style (com.example.myqueue
). This parameter is optional and can be NULL
.
qos The service quality level associated with the queue. This value determines the priority of tasks scheduled by the system. See DispatchQoS.QoSClass (https://developer.apple.com/documentation/dispatch/dispatchqos/qosclass) for a list of configurable values.
attributes
The attribute associated with the queue. You can include the concurrent
attribute to create a dispatch queue that runs tasks in parallel. If you omit this attribute, the dispatch queue runs the tasks in series.
autoreleaseFrequency How often the queue automatically releases "objects created by scheduled blocks". See DispatchQueue.AutoreleaseFrequency for a list of possible values.
--. Inherit
(inherits the auto-release frequency of the target queue)
--. WorkItem
(released after the block finishes executing)
--. Never
(The block will not be auto-released even after execution.)
DISPATCH_TARGET_QUEUE_DEFAULT
if you want to provide the system with a" queue suitable for the current object ".Example of creating a private background queue
let videoDataOutputQueue = DispatchQueue(label: "VideoDataOutput",
qos: .userInitiated,
attributes: [],
autoreleaseFrequency: .workItem)
Recommended Posts