grand-central-dispatch
Grand Central Dispatch vs NSThreads?
Advantages of Dispatch The advantages of dispatch are mostly outlined here: Migrating Away from Threads The idea is that you eliminate work on your part, since the paradigm fits MOST code more easily. It reduces the memory penalty your application pays for storing thread stacks in the application’s memory space. It eliminates the code needed … Read more
Possible to reset state of dispatch_once in unit test, to make them run again
I should note first that this isn’t a good thing to do in any situation other than testing; even then, proceed with care — AliSoftware provides some details and example code in comments below. See also the interesting answers at Can I declare a dispatch_once_t predicate as a member variable instead of static?, including some … 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
How to stop a DispatchWorkItem in GCD?
GCD does not perform preemptive cancelations. So, to stop a work item that has already started, you have to test for cancelations yourself. In Swift, cancel the DispatchWorkItem. In Objective-C, call dispatch_block_cancel on the block you created with dispatch_block_create. You can then test to see if was canceled or not with isCancelled in Swift (known … Read more
NSURLConnection and grand central dispatch
I recommend you to see WWDC Sessions about network application in iPhone OS. WWDC 2010 Session 207 – Network Apps for iPhone OS, Part 1 WWDC 2010 Session 208 – Network Apps for iPhone OS, Part 2 The lecturer said “Threads Are Evil™” for network programming and recommended to use asynchronous network programming with RunLoop. … Read more
OperationQueue.main vs DispatchQueue.main
For details on the differences between the two types of queue, see Lion’s answer. Both approaches will work. However, NSOperation is mostly needed when more advanced scheduling (including dependencies, canceling, etc.) is required. So in this case, a simple DispatchQueue.main.async { /* do work */ } will be just fine. That would be equivalent to … Read more