GCD
is a low-level C-based API that enables very simple use of a task-based concurrency model. NSOperation
and NSOperationQueue
are Objective-C classes that do a similar thing. NSOperation
was introduced first, but as of 10.5 and iOS 2, NSOperationQueue
and friends are internally implemented using GCD
.
In general, you should use the highest level of abstraction that suits your needs. This means that you should usually use NSOperationQueue
instead of GCD
, unless you need to do something that NSOperationQueue
doesn't support.
Note that NSOperationQueue
isn't a "dumbed-down" version of GCD; in fact, there are many things that you can do very simply with NSOperationQueue
that take a lot of work with pure GCD
. (Examples: bandwidth-constrained queues that only run N operations at a time; establishing dependencies between operations. Both very simple with NSOperation
, very difficult with GCD
.) Apple's done the hard work of leveraging GCD to create a very nice object-friendly API with NSOperation
. Take advantage of their work unless you have a reason not to.
Caveat:
On the other hand, if you really just need to send off a block, and don't need any of the additional functionality that NSOperationQueue
provides, there's nothing wrong with using GCD. Just be sure it's the right tool for the job.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…