Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
453 views
in Technique[技术] by (71.8m points)

ios - GCD vs performSelectorInBackground/performSelectorOnMainThread

I am new in ios development. I have following questions:

  1. When we use GCD(dispatch_group_async, dispatch_async(dispatch_get_main_queue()...) and when we use performSelectorInBackground/performSelectorOnMainThread?
  2. What's the differences between those two.

    I know when we use performSelectorInBackground, we create a new NSThread. But isn't the same when we use dispatch_group_async? Because if we create more than one dispatch_group_async, it means we need to submit more than one blocks in queue. And those blocks might run on different queues. Therefore, when we create more than one dispatch_group_async, does it means we create a new thread? (because blocks may run on different queues) (I kind of confused about NSThread and block queue.....)

Thanks!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When to use performSelectorInBackground:

Never. Do not use this method. It spawns an unbounded number of threads. Even before GCD was available, this was a horrible method.

When to use performSelectorOnMainThread:

Meh… Never, but just because it's inconvenient. There's nothing deeply wrong with this method. It's just not as useful as dispatch_async().

The difference between GCD and the old performSelector… methods (and NSThread in general) is that GCD manages a thread pool for you. In general, you should avoid manual threading in Cocoa. Instead, use NSOperationQueue or GCD (dispatch methods). They provide a more useful queue abstraction rather than forcing you to manually manage threads.

Be sure to read over Apple's Migrating Away from Threads for more info.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...