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
3,762
questions
776
votes
20
answers
399k
views
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?
585
votes
26
answers
295k
views
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: ...
492
votes
9
answers
134k
views
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 ...
485
votes
9
answers
247k
views
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)))
...
420
votes
15
answers
379k
views
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.
...
348
votes
10
answers
99k
views
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 ...
288
votes
4
answers
149k
views
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 ...
263
votes
6
answers
214k
views
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(...
212
votes
11
answers
194k
views
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 ...
205
votes
10
answers
124k
views
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 = [...
203
votes
10
answers
136k
views
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 ...
184
votes
13
answers
126k
views
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 ...
147
votes
7
answers
189k
views
Waiting until the task finishes
How could I make my code wait until the task in DispatchQueue finishes? Does it need any completion handler or something?
func myFunction() {
var a: Int?
DispatchQueue.main.async {
...
146
votes
6
answers
146k
views
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 ...
141
votes
6
answers
68k
views
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 ...
133
votes
3
answers
47k
views
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 ...
104
votes
7
answers
56k
views
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 ...
97
votes
2
answers
27k
views
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 ...
95
votes
12
answers
59k
views
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(&...
83
votes
3
answers
42k
views
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?
76
votes
8
answers
51k
views
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!
73
votes
1
answer
11k
views
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 ...
72
votes
5
answers
34k
views
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 ...
67
votes
12
answers
65k
views
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 ...
67
votes
10
answers
82k
views
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?
65
votes
4
answers
43k
views
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 "...
65
votes
3
answers
22k
views
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()...
62
votes
1
answer
18k
views
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 ...
61
votes
6
answers
34k
views
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 ...
59
votes
7
answers
87k
views
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 ...
59
votes
7
answers
38k
views
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.
...
57
votes
3
answers
23k
views
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 ...
56
votes
6
answers
12k
views
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 ...
53
votes
6
answers
45k
views
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....
53
votes
3
answers
56k
views
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 ...
50
votes
4
answers
15k
views
(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(), ...
49
votes
8
answers
116k
views
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)...
49
votes
1
answer
35k
views
Unable to access global variables in dispatch_async : "Variable is not Assignable (missing _block type specifier)" [duplicate]
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(...
49
votes
5
answers
16k
views
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 ...
49
votes
3
answers
17k
views
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 ...
48
votes
5
answers
63k
views
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(...
47
votes
8
answers
105k
views
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, ...
46
votes
5
answers
23k
views
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 ...
45
votes
1
answer
29k
views
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 ...
43
votes
3
answers
25k
views
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 ...
42
votes
5
answers
23k
views
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 ...
42
votes
2
answers
36k
views
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....
42
votes
6
answers
15k
views
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 ...
40
votes
3
answers
25k
views
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 ...
40
votes
2
answers
7k
views
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 ...