GCD, NSOperationQueue, or create a thread manually?

余生颓废 提交于 2019-12-03 12:38:33

问题


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:

  1. Asynchronous method calls
  2. NSOperationQueue
  3. Grand Central Dispatch
  4. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!