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
58
votes
7 answers

Whither dispatch_once in Swift 3?

Okay, so I found out about the new Swifty Dispatch API in Xcode 8. I'm having fun using DispatchQueue.main.async, and I've been browsing around the Dispatch module in Xcode to find all the new APIs. But I also use dispatch_once to make sure that…
rickster
  • 124,678
  • 26
  • 272
  • 326
57
votes
3 answers

What's the difference between performSelectorOnMainThread: and dispatch_async() on main queue?

I was having problems modifying a view inside a thread. I tried to add a subview but it took around 6 or more seconds to display. I finally got it working, but I don't know how exactly. So I was wondering why it worked and what's the difference…
53
votes
3 answers

dispatch_sync vs. dispatch_async on main queue

Bear with me, this is going to take some explaining. I have a function that looks like the one below. Context: "aProject" is a Core Data entity named LPProject with an array named 'memberFiles' that contains instances of another Core Data entity…
53
votes
5 answers

RunLoop vs DispatchQueue as Scheduler

When using new Combine framework you can specify the scheduler on which to receive elements from the publisher. Is there a big difference between RunLoop.main and DispatchQueue.main in this case when assigning publisher to UI element? The first one…
mikro098
  • 2,173
  • 2
  • 32
  • 48
52
votes
6 answers

When to use Semaphore instead of Dispatch Group?

I would assume that I am aware of how to work with DispatchGroup, for understanding the issue, I've tried: class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() performUsingGroup() } …
Ahmad F
  • 30,560
  • 17
  • 97
  • 143
50
votes
4 answers

(iOS) dispatch_async() vs. NSOperationQueue

I learned iOS programming thanks to Stanford's CS193p course (on iTunes U) as well as the iOS programming book from Big Nerd Ranch. In both of those, they recommend using dispatch_async(), dispatch_get_main_queue(), etc. to handle threading and…
Donald Burr
  • 2,281
  • 2
  • 23
  • 31
49
votes
3 answers

Grand Central Dispatch (GCD) vs. performSelector - need a better explanation

I've used both GCD and performSelectorOnMainThread:waitUntilDone in my apps, and tend to think of them as interchangeable--that is, performSelectorOnMainThread:waitUntilDone is an Obj-C wrapper to the GCD C syntax. I've been thinking of these two…
akaru
  • 6,299
  • 9
  • 63
  • 102
49
votes
8 answers

Does dispatch_async(dispatch_get_main_queue(), ^{...}); wait until done?

I have a scenario in my app, where I want to do some time consuming task which consists of some data processing as well as UI update, in a method. My method looks like this, - (void)doCalculationsAndUpdateUIs { // DATA PROCESSING 1 // UI…
EmptyStack
  • 51,274
  • 23
  • 147
  • 178
49
votes
1 answer

Unable to access global variables in dispatch_async : "Variable is not Assignable (missing _block type specifier)"

In My dispach_async code block I cannot access global variables. I am getting this error Variable is not Assignable (missing _block type specifier). NSString *textString; dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, …
48
votes
5 answers

How do you schedule a block to run on the next run loop iteration?

I want to be able to execute a block on the next run loop iteration. It's not so important whether it gets executed at the beginning or the end of the next run loop, just that execution is deferred until all code in the current run loop has finished…
lmirosevic
  • 15,787
  • 13
  • 70
  • 116
47
votes
8 answers

EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose

I am getting EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0) on dispatch_semaphore_dispose but don't really know how to track down the root cause of this. My code makes use of dispatch_async, dispatch_group_enter and so on. UPDATE: The cause…
Boon
  • 40,656
  • 60
  • 209
  • 315
47
votes
5 answers

dispatch_get_global_queue vs dispatch_get_main_queue

Starting to learn about core data and dispatch_async. There is a block of code to get url of image from set of data and set it to model of core data like below dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ …
tranvutuan
  • 6,089
  • 8
  • 47
  • 83
46
votes
5 answers

dispatch_sync on main queue hangs in unit test

I was having some trouble unit testing some grand central dispatch code with the built in Xcode unit testing framework, SenTestingKit. I managed to boil my problem done to this. I have a unit test that builds a block and tries to execute it on the…
Drewsmits
  • 1,384
  • 2
  • 14
  • 24
45
votes
1 answer

DispatchQueue : Cannot be called with asCopy = NO on non-main thread

I am presenting the UIAlertController on the main thread as : class HelperMethodClass: NSObject { class func showAlertMessage(message:String, viewController: UIViewController) { let alertMessage = UIAlertController(title: "", message:…
Amit
  • 4,837
  • 5
  • 31
  • 46
42
votes
6 answers

Core Data and threads / Grand Central Dispatch

I'm a beginner with Grand Central Dispatch (GCD) and Core Data, and I need your help to use Core Data with CGD, so that the UI is not locked while I add 40.000 records to Core Data. I know that CD is not thread safe, so I have to use another…
Rui Lopes
  • 2,562
  • 6
  • 34
  • 49