Questions tagged [grand-central-dispatch]

Grand Central Dispatch (GCD) provides a simple and robust mechanism for concurrent and asynchronous operations, primarily in Apple operating systems (e.g., iOS, macOS, watchOS, and tvOS), but also FreeBSD and MidnightBSD.

Grand Central Dispatch (GCD) provides a simple and robust mechanism for concurrent operations in Apple platforms including iOS, macOS, watchOS, and tvOS. This is described in Apple's Introducing Blocks and Grand Central Dispatch:

GCD combines an easy-to-use programming model with highly-efficient system services to radically simplify the code needed to make best use of multiple processors. ...

The central insight of GCD is shifting the responsibility for managing threads and their execution from applications to the operating system. As a result, programmers can write less code to deal with concurrent operations in their applications, and the system can perform more efficiently on single-processor machines, large multiprocessor servers, and everything in between. Without a pervasive approach such as GCD, even the best-written application cannot deliver the best possible performance, because it doesn’t have full insight into everything else happening in the system.

GCD is implemented as a set of extensions to the C language as well as a new API and runtime engine. While initially inspired by the challenge of multicore computing, these actually solve a more general problem: how to efficiently schedule multiple independent chunks of work.

The technology is developed by Apple, and the supporting runtime library is released as open-source.

Alternatives

There are a few native alternatives:

  • Operation queues: Operation queues are built upon GCD technology, but offer more elegant cancelation, priorities, dependencies, and the ability to wrap asynchronous tasks in operations.

  • Combine: The Combine framework provides a declarative Swift API for processing values over time. These values can represent many kinds of asynchronous events. Combine declares publishers to expose values that can change over time, and subscribers to receive those values from the publishers.

  • async-await: Swift concurrency eliminates the completion handler pattern and offers a simpler pattern for representing asynchronous patterns, error handling, etc.

Resources:

3748 questions
133
votes
3 answers

Difference between dispatch_async and dispatch_sync on serial queue?

I've created a serial queue like this: dispatch_queue_t _serialQueue = dispatch_queue_create("com.example.name", DISPATCH_QUEUE_SERIAL); What's the difference between dispatch_async called like this dispatch_async(_serialQueue, ^{ /* TASK 1 */…
JRG-Developer
  • 12,454
  • 8
  • 55
  • 81
104
votes
7 answers

Alternatives to dispatch_get_current_queue() for completion blocks in iOS 6?

I have a method that accepts a block and a completion block. The first block should run in the background, while the completion block should run in whatever queue the method was called. For the latter I always used dispatch_get_current_queue(), but…
cfischer
  • 24,452
  • 37
  • 131
  • 214
97
votes
2 answers

Does ARC support dispatch queues?

I'm reading apple's documentation about "Memory Management for Dispatch Queues": Even if you implement a garbage-collected application, you must still retain and release your dispatch queues and other dispatch objects. Grand Central Dispatch does…
flagg19
  • 1,782
  • 2
  • 22
  • 27
93
votes
12 answers

dispatch_once after the Swift 3 GCD API changes

What is the new syntax for dispatch_once in Swift after the changes made in language version 3? The old version was as follows. var token: dispatch_once_t = 0 func test() { dispatch_once(&token) { } } These are the changes to libdispatch…
David
  • 14,205
  • 20
  • 97
  • 144
80
votes
3 answers

Operation Queue vs Dispatch Queue for iOS Application

What are the differences between Operation Queue and Dispatch Queue? Under what circumstances will it be more appropriate to use each?
Lopper
  • 3,499
  • 7
  • 38
  • 57
76
votes
8 answers

using dispatch_sync in Grand Central Dispatch

Can anyone explain with really clear use cases what the purpose of dispatch_sync in GCD is for? I can't understand where and why I would have to use this. Thanks!
Rasputin Jones
  • 1,427
  • 2
  • 16
  • 24
72
votes
1 answer

Do you need to create an NSAutoreleasePool within a block in GCD?

Normally, if you spawn a background thread or run an NSOperation on an NSOperationQueue you need to create an NSAutoreleasePool for that thread or operation because none exists by default. Does the same rule apply to a block that is placed within…
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
70
votes
5 answers

Is DispatchQueue.global(qos: .userInteractive).async same as DispatchQueue.main.async

I was going through the tutorial : https://www.raywenderlich.com/148513/grand-central-dispatch-tutorial-swift-3-part-1 And came across the definition of QoS class User-interactive. Its mentioned there that this should run on main thread. So, my…
Nishu_Priya
  • 1,251
  • 1
  • 10
  • 23
67
votes
10 answers

How to stop the execution of tasks in a dispatch queue?

If I have a serial queue, how can I, from the main thread, tell it to immediately stop execution and cancel all of its tasks?
Randall
  • 2,083
  • 2
  • 18
  • 20
65
votes
4 answers

What's the difference between the "global queue" and the "main queue" in GCD?

Among some other ways, there are these two ways to get queues in GCD: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); dispatch_get_main_queue(); If I'm not completely wrong, the "main queue" is executing on the main thread and is…
Proud Member
  • 40,078
  • 47
  • 146
  • 231
65
votes
12 answers

Get current dispatch queue?

I have a method which should support being called from any queue, and should expect to. It runs some code in a background thread itself, and then uses dispatch_get_main_queue when it returns a value to its block argument. I don't want it to force it…
Andrew
  • 15,935
  • 28
  • 121
  • 203
65
votes
3 answers

How to dispatch on main queue synchronously without a deadlock?

I need to dispatch a block on the main queue, synchronously. I don’t know if I’m currently running on the main thread or no. The naive solution looks like this: dispatch_sync(dispatch_get_main_queue(), block); But if I’m currently inside of a block…
zoul
  • 102,279
  • 44
  • 260
  • 354
60
votes
1 answer

Difference between DispatchSourceTimer, Timer and asyncAfter?

I am struggling to understand the key differences between DispatchSourceTimer, Timer and asyncAfter (in my case for scheduling a task that needs to be ran every X seconds, although understanding the differences in timers can be useful to) (Or is…
J. Doe
  • 12,159
  • 9
  • 60
  • 114
60
votes
6 answers

Why can't we use a dispatch_sync on the current queue?

I ran into a scenario where I had a delegate callback which could occur on either the main thread or another thread, and I wouldn't know which until runtime (using StoreKit.framework). I also had UI code that I needed to update in that callback…
59
votes
7 answers

What's the difference between synchronous and asynchronous calls in Objective-C, versus multi-threading?

For the longest time I thought asynchronous was synonymous to running something on a background thread, while synchronous meant on the main thread (blocking UI updates and interactions). I understand that not running on the main thread for expensive…
Doug Smith
  • 29,668
  • 57
  • 204
  • 388