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.

Resources:

3612 questions
755
votes
19 answers

How do you trigger a block after a delay, like -performSelector:withObject:afterDelay:?

Is there a way to call a block with a primitive parameter after a delay, like using performSelector:withObject:afterDelay: but with an argument like int/double/float?
Egil
  • 7,881
  • 4
  • 15
  • 6
571
votes
26 answers

dispatch_after - GCD in Swift?

I've gone through the iBook from Apple, and couldn't find any definition of it: Can someone explain the structure of dispatch_after? dispatch_after(<#when: dispatch_time_t#>, <#queue: dispatch_queue_t?#>, <#block: dispatch_block_t?#>)
Kumar KL
  • 15,086
  • 9
  • 36
  • 57
475
votes
8 answers

NSOperation vs Grand Central Dispatch

I'm learning about concurrent programming for iOS. So far I've read about NSOperation/NSOperationQueue and GCD. What are the reasons for using NSOperationQueue over GCD and vice versa? Sounds like both GCD and NSOperationQueue abstract away the…
462
votes
12 answers

How do I write dispatch_after GCD in Swift 3, 4, and 5?

In Swift 2, I was able to use dispatch_after to delay an action using grand central dispatch: var dispatchTime: dispatch_time_t = dispatch_time(DISPATCH_TIME_NOW, Int64(0.1 * Double(NSEC_PER_SEC))) dispatch_after(dispatchTime,…
brandonscript
  • 57,554
  • 29
  • 142
  • 204
409
votes
15 answers

How to create dispatch queue in Swift 3

In Swift 2, I was able to create queue with the following code: let concurrentQueue = dispatch_queue_create("com.swift3.imageQueue", DISPATCH_QUEUE_CONCURRENT) But this doesn't compile in Swift 3. What is the preferred way to write this in Swift…
user4790024
343
votes
10 answers

Create singleton using GCD's dispatch_once in Objective-C

If you can target iOS 4.0 or above Using GCD, is it the best way to create singleton in Objective-C (thread safe)? + (instancetype)sharedInstance { static dispatch_once_t once; static id sharedInstance; dispatch_once(&once, ^{ …
Ryan
  • 9,231
  • 23
  • 77
  • 133
266
votes
4 answers

GCD to perform task in main thread

I have a callback which might come from any thread. When I get this callback, then I would like to perform a certain task on the main thread. Do I need to check whether I already am on the main thread - or is there any penalty by not performing this…
Egil
  • 3,967
  • 4
  • 18
  • 13
252
votes
6 answers

How do I dispatch_sync, dispatch_async, dispatch_after, etc in Swift 3, Swift 4, and beyond?

I have lots of code in Swift 2.x (or even 1.x) projects that looks like this: // Move to a background thread to do some long running work dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) { let image =…
rickster
  • 118,448
  • 25
  • 255
  • 308
199
votes
9 answers

In Swift how to call method with parameters on GCD main thread?

In my app I have a function that makes an NSRURLSession and sends out an NSURLRequest using sesh.dataTaskWithRequest(req, completionHandler: {(data, response, error) In the completion block for this task, I need to do some computation that adds a…
almel
  • 5,614
  • 10
  • 36
  • 52
198
votes
10 answers

Waiting until two async blocks are executed before starting another block

When using GCD, we want to wait until two async blocks are executed and done before moving on to the next steps of execution. What is the best way to do that? We tried the following, but it doesn't seem to…
tom
  • 13,143
  • 18
  • 63
  • 120
184
votes
13 answers

How do I wait for an asynchronously dispatched block to finish?

I am testing some code that does asynchronous processing using Grand Central Dispatch. The testing code looks like this: [object runSomeLongOperationAndDo:^{ STAssert… }]; The tests have to wait for the operation to finish. My current solution…
zoul
  • 96,282
  • 41
  • 242
  • 342
179
votes
8 answers

Wait until swift for loop with asynchronous network requests finishes executing

I would like a for in loop to send off a bunch of network requests to firebase, then pass the data to a new view controller once the the method finishes executing. Here is my code: var datesArray = [String: AnyObject]() for key in locationsArray { …
Josh
  • 2,225
  • 3
  • 10
  • 22
145
votes
6 answers

iPhone - Grand Central Dispatch main thread

I have been using with success, grand central dispatch in my apps, but I was wondering what is the real advantage of using something like this: dispatch_async(dispatch_get_main_queue(), ^{ ... do stuff or…
Duck
  • 32,792
  • 46
  • 221
  • 426
130
votes
6 answers

Concurrent vs serial queues in GCD

I'm struggling to fully understand the concurrent and serial queues in GCD. I have some issues and hoping someone can answer me clearly and at the point. I'm reading that serial queues are created and used in order to execute tasks one after the…
Bogdan Alexandru
  • 5,034
  • 6
  • 31
  • 53
127
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
  • 11,675
  • 8
  • 54
  • 79
1
2 3
99 100