GCD, NSOperationQueue, or create a thread manually?
Asked Answered
K

2

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?

Kweisui answered 22/8, 2011 at 10:23 Comment(0)
N
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.

Nogood answered 22/8, 2011 at 10:45 Comment(3)
thanks, but what do you mean by your n°1 asynchronous method calls? i think i'm only aware of the last 3 methods.Kweisui
okay, i'll take your advice. I also found this good discussion about threads and nsoperationQueue : #3042337 . But still, is there a way to make asynchronous calls, as you said in your post?Kweisui
Any method that doesn't immediately return results but does so using delegate methods (or notifications) is asynchronous.Nogood
J
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.

Jag answered 22/8, 2011 at 10:54 Comment(8)
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 synthaxKweisui
NSOperationQueue is implemented using GCD (though you don't need to know that to use it).Nogood
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.".Jag
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.Jag
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.Jag
@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 againKweisui
developer.apple.com/videos/wwdc/2010 for 2010 sessions and 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.Jag
@Sepehr Mahmoudian : Wow, it's a gold mine, i began to walk in the desert but i found an oasis! thanks for the links!Kweisui

© 2022 - 2024 — McMap. All rights reserved.