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
42
votes
2 answers

Using dispatch_async with self

I have run into this problem a few times while porting Objective-C code to Swift. Say I have the following code: dispatch_async(dispatch_get_main_queue()) { self.hostViewController?.view.addSubview(self.commandField) } This will result in an…
Ephemera
  • 8,672
  • 8
  • 44
  • 84
42
votes
3 answers

What is the difference between dispatch_get_global_queue and dispatch_queue_create?

I'm writing a moderately complex iOS program that needs to have multiple threads for some of its longer operations (parsing, connections to the network...etc). However, I'm confused as to what the difference is between dispatch_get_global_queue and…
Nosrettap
  • 10,940
  • 23
  • 85
  • 140
41
votes
5 answers

What property should I use for a Dispatch Queue after ARC?

I maintain a dispatch queue as a property with my view controller. I create this queue once in my view controller's init method, and reuse a few times for some background tasks. Before ARC, I was doing this: @property (nonatomic, assign)…
Z S
  • 7,039
  • 12
  • 53
  • 105
40
votes
3 answers

How to stop a DispatchWorkItem in GCD?

I am currently playing around with Grand Central Dispatch and discovered a class called DispatchWorkItem. The documentation seems a little incomplete so I am not sure about using it the right way. I created the following snippet and expected…
40
votes
2 answers

How can I verify that I am running on a given GCD queue without using dispatch_get_current_queue()?

Recently, I had the need for a function that I could use to guarantee synchronous execution of a given block on a particular serial dispatch queue. There was the possibility that this shared function could be called from something already running on…
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
38
votes
3 answers

Wait for async task to finish completion block before returning in app delegate

I'm using a subclass of UIManagedDocument to use Core Data in my project. The point is for the subclass to return a singleton instance so that my screens can simply call it and the managed object context remains the same for all of them. Before…
Matthew Quiros
  • 13,385
  • 12
  • 87
  • 132
38
votes
2 answers

How can I replace deprecated method dispatch_get_current_queue()?

I am developing a chat application using xmppframework in iOS 5; it works perfectly. But I updated my Xcode to 4.5.1, iOS 5 to iOS 6 and my Mac OS to 10.7.5, and the project did not work due to deprecation issues. I replaced all methods with new…
venkat
  • 762
  • 2
  • 8
  • 20
37
votes
3 answers

C++11 Thread safety of Random number generators

In C++11 there are a bunch of new Random number generator engines and distribution functions. Are they thread safe? If you share a single random distribution and engine among multiple threads, is it safe and will you still receive random numbers?…
user1139069
  • 1,505
  • 2
  • 15
  • 27
37
votes
2 answers

iOS GCD: Difference between any global queue and the one with background priority (DISPATCH_QUEUE_PRIORITY_BACKGROUND)?

I am reading Concurrency Programming Guide and things confuse me. I see a lot of code invoking the following for any background task: dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); Now what I mean by 'background' is the popular…
Nirav Bhatt
  • 6,940
  • 5
  • 45
  • 89
36
votes
3 answers

OperationQueue.main vs DispatchQueue.main

When you need to perform something on the main thread in the completion block of a networking task or an operation, which of these ways to get it would be the most appropriate and why?: OperationQueue.main.addOperation DispatchQueue.main.async
AppsDev
  • 12,319
  • 23
  • 93
  • 186
35
votes
3 answers

NSURLConnection and grand central dispatch

Is it advisable to wrap up NSUrlConnection in a gcd style blocks and run it on a low_priority queue? I need to ensure that my connections are not happening on the main thread and the connections need to be asynchronous. I also need several…
Jonas Anderson
  • 1,987
  • 4
  • 24
  • 28
34
votes
5 answers

Why should I choose GCD over NSOperation and blocks for high-level applications?

Apple's Grand Central Dispatch reference says: "...if your application needs to operate at the Unix level of the system—for example, if it needs to manipulate file descriptors, Mach ports, signals, or timers. GCD is not restricted to…
Lio
  • 4,225
  • 4
  • 33
  • 40
34
votes
6 answers

Swift throw from closure nested in a function

I have a function that throws an error, in this function I have a inside a closure that I need to throw the error from it's completion handler. Is that possible ? Here is my code so far. enum CalendarEventError: ErrorType { case UnAuthorized …
shannoga
  • 19,649
  • 20
  • 104
  • 169
34
votes
3 answers

Dispatch queues: How to tell if they're running and how to stop them

I'm just playing around with GCD and I've written a toy CoinFlipper app. Here's the method that flips the coins: - (void)flipCoins:(NSUInteger)nFlips{ // Create the queues for work dispatch_queue_t mainQueue = dispatch_get_main_queue(); …
Abizern
  • 146,289
  • 39
  • 203
  • 257
34
votes
1 answer

Workaround on the threads limit in Grand Central Dispatch?

With Grand Central Dispatch, one can easily perform time consuming task on non-main thread, avoid blocking the main thead and keep the UI responsive. Simply by using dispatch_async and perform the task on a global concurrent…
howanghk
  • 3,070
  • 2
  • 21
  • 34