9

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?

User97693321
  • 3,336
  • 7
  • 45
  • 69
Paul
  • 6,108
  • 14
  • 72
  • 128

2 Answers2

10

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.

Stephen Darlington
  • 51,577
  • 12
  • 107
  • 152
  • thanks, but what do you mean by your n°1 asynchronous method calls? i think i'm only aware of the last 3 methods. – Paul Aug 22 '11 at 15:16
  • okay, i'll take your advice. I also found this good discussion about threads and nsoperationQueue : http://stackoverflow.com/questions/3041837/nsthread-vs-nsoperationqueue-vs-on-the-iphone . But still, is there a way to make asynchronous calls, as you said in your post? – Paul Aug 22 '11 at 17:16
  • 1
    Any method that doesn't immediately return results but does so using delegate methods (or notifications) is asynchronous. – Stephen Darlington Aug 22 '11 at 17:37
3

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.

  • thanks Sepehr, i think i read that GCD uses the C language with "dispatch_queue", which is an equivalent of NSOperationQueue? so when you say GCD, do you mean also the objective-c methods or the C methods? i'm not quite "comfortable" with C synthax – Paul Aug 22 '11 at 15:20
  • `NSOperationQueue` is implemented using GCD (though you don't need to know that to use it). – Stephen Darlington Aug 22 '11 at 17:38
  • in iOS NSOperationQueue is NOT implemented using GCD, from Apple's NSOperationQueue Class Reference : "In iOS, operation queues do not use Grand Central Dispatch to execute operations.". – Sepehr Mahmoudian Aug 23 '11 at 04:07
  • 3
    GCD is superior to other technologies because: 1. It cuts development time 2. The system manages threads, it reuses previously created threads if possible. 3. It has access to resources allocated for the system, in most cases it will be able to provide you with more resources than you would have if you had used other threading technologies. 4. You do not need to use locks that end up making your program actually run slower in most cases. 5. GCD lets you use multiple processing cores easily. – Sepehr Mahmoudian Aug 23 '11 at 04:12
  • 1
    you need to use C language to work with GCD and you will also need to know how to work with blocks. but you will only need to spend about half an hour to learn both. – Sepehr Mahmoudian Aug 23 '11 at 04:14
  • @Sepehr Mahmoudian : thanks, half an hour to learn GCD from the doc? i usually need more time to understand and use a new thing! :) thanks for the messages, do you have any example about GCD i could download, any link? thanks again – Paul Aug 23 '11 at 09:38
  • http://developer.apple.com/videos/wwdc/2010/ for 2010 sessions and http://developer.apple.com/videos/wwdc/2011/ for 2011 sessions. I suggest you start by viewing the 2010 talk about GCD then proceed to the ones in 2011, i think there were 2-3 this year. – Sepehr Mahmoudian Aug 23 '11 at 09:49
  • @Sepehr Mahmoudian : Wow, it's a gold mine, i began to walk in the desert but i found an oasis! thanks for the links! – Paul Aug 24 '11 at 15:13