GCD vs performSelectorInBackground/performSelectorOnMainThread

后端 未结 2 777
天涯浪人
天涯浪人 2020-12-08 05:30

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 per
2条回答
  •  萌比男神i
    2020-12-08 05:52

    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.

提交回复
热议问题