dispatch_sync vs. dispatch_async on main queue

This is a common issue related to disk I/O and GCD. Basically, GCD is probably spawning one thread for each file, and at a certain point you’ve got too many threads for the system to service in a reasonable amount of time. Every time you call dispatch_async() and in that block you attempt to to … Read more

Whither dispatch_once in Swift 3?

Since Swift 1.x, Swift has been using dispatch_once behind the scenes to perform thread-safe lazy initialization of global variables and static properties. So the static var above was already using dispatch_once, which makes it sort of weird (and possibly problematic to use it again as a token for another dispatch_once. In fact there’s really no … Read more

Difference between DispatchSourceTimer, Timer and asyncAfter?

Timer is a Swift bridge of NSTimer, which goes back to NeXTSTEP, long, long before Grand Central Dispatch (GCD) and things like DispatchSourceTimer, which didn’t come along until 10.6 (in the form of dispatch_source_set_timer) and dispatchAfter (in the form of dispatch_after). NSTimer is based on the run loop, which was the primary way that concurrency … Read more

What’s the difference between synchronous and asynchronous calls in Objective-C, versus multi-threading?

When you invoke something synchronously, it means that the thread that initiated that operation will wait for the task to finish before continuing. Asynchronous means that it will not wait. Having said that, when people suggest that you perform some slow or expensive process asynchronously, they are implicitly suggesting not only that you should run … 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

How to dispatch on main queue synchronously without a deadlock?

I need to use something like this fairly regularly within my Mac and iOS applications, so I use the following helper function (originally described in this answer): void runOnMainQueueWithoutDeadlocking(void (^block)(void)) { if ([NSThread isMainThread]) { block(); } else { dispatch_sync(dispatch_get_main_queue(), block); } } which you call via runOnMainQueueWithoutDeadlocking(^{ //Do stuff }); This is pretty much … Read more

What’s the difference between the “global queue” and the “main queue” in GCD?

The main queue does indeed run on the main thread like you say. The global queues are concurrent queues and from the main page for dispatch_get_global_queue: Unlike the main queue or queues allocated with dispatch_queue_create(), the global concurrent queues schedule blocks as soon as threads become available (“non-FIFO” completion order). The global concurrent queues represent … Read more

How to stop the execution of tasks in a dispatch queue?

If you’re using Swift the DispatchWorkItem class allows works units to be cancelled individually. Work items allow you to configure properties of individual units of work directly. They also allow you to address individual work units for the purposes of waiting for their completion, getting notified about their completion, and/or canceling them. ( available for … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)