问题
When you use threads, do you have any preferences? In general rule, to use any of these techniques :
- create a new thread manually and use the run loop
- use NSOperationQueue
- or use Grand Central Dispatch and the C version with dispatch_queue?
Does NSOperationQueue simplify everything, and thus is better to be used when we need to create an asynchronous function?
回答1:
I'm lazy, so my philosophy is to pick the simplest solution that does everything I need it to. (I like to think this is the "lazy" espoused by Larry Wall but sometimes I wonder.)
So my order of preference would be:
- Asynchronous method calls
NSOperationQueue
- Grand Central Dispatch
- Thread
There an increase in complexity and flexibility with each step down. If you need the extra flexibility then the complexity is probably worth it.
回答2:
I can remember that in a WWDC 2010 session it was said that GCD is the way to go unless you are working with APIs that currently do not work well with it.
As a general rule, I always use asynchronous method calls for networking and avoid using pthreads or NSThreads directly unless it is absolutely necessary.
来源:https://stackoverflow.com/questions/7146052/gcd-nsoperationqueue-or-create-a-thread-manually