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

The pattern of “run on whatever queue the caller was on” is appealing, but ultimately not a great idea. That queue could be a low priority queue, the main queue, or some other queue with odd properties. My favorite approach to this is to say “the completion block runs on an implementation defined queue with … Read more

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

This is a semi-common question when programming with GCD. The short answer is that GCD does not have a cancelation API for queues. The rationale: memory management would become vastly more complicated, because a given block might be responsible for free()ing a given allocation of memory. By always running the block, GCD ensures that memory … Read more

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

No it doesn’t wait and the way you are doing it in that sample is not good practice. dispatch_async is always asynchronous. It’s just that you are enqueueing all the UI blocks to the same queue so the different blocks will run in sequence but parallel with your data processing code. If you want the … Read more

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

dispatch_sync does two things: queue a block blocks the current thread until the block has finished running Given that the main thread is a serial queue (which means it uses only one thread), if you run the following statement on the main queue: dispatch_sync(dispatch_get_main_queue(), ^(){/*…*/}); the following events will happen: dispatch_sync queues the block in … Read more

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

Modern versions of Swift use DispatchQueue.main.async to dispatch to the main thread: DispatchQueue.main.async { // your code here } To dispatch after on the main queue, use: DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) { // your code here } Older versions of Swift used: dispatch_async(dispatch_get_main_queue(), { let delegateObj = UIApplication.sharedApplication().delegate as YourAppDelegateClass delegateObj.addUIImage(“yourstring”) })

Difference between dispatch_async and dispatch_sync on serial queue?

Yes. Using serial queue ensure the serial execution of tasks. The only difference is that dispatch_sync only return after the block is finished whereas dispatch_async return after it is added to the queue and may not finished. for this code dispatch_async(_serialQueue, ^{ printf(“1”); }); printf(“2”); dispatch_async(_serialQueue, ^{ printf(“3”); }); printf(“4”); It may print 2413 or … Read more

Adding items to Swift array across multiple threads causing issues (because arrays aren’t thread safe) – how do I get around that?

You have defined your blocksDispatchQueue to be a global queue. Updating this for Swift 3, the equivalent is: private let queue = DispatchQueue.global() func addBlockToArray(block: @escaping () -> Void) { queue.async { self.blocksArray.append(block) } } The problem is that global queues are concurrent queues, so you’re not achieving the synchronization you want. But if you … Read more