Why should I choose GCD over NSOperation and blocks for high-level applications?

前端 未结 5 1204
既然无缘
既然无缘 2020-12-07 12:19

Apple\'s Grand Central Dispatch reference says:

\"...if your application needs to operate at the Unix level of the system—for example, if it needs t

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-07 13:11

    • Prefer GCD where task is not much complex and optimum CPU performance is required.
    • Prefer NSOperationQueue where task is complex and requires canceling or suspending a block and dependency management.

    GCD is a lightweight way to represent units of work that are going to be executed concurrently. You don’t schedule these units of work; the system takes care of scheduling for you. Adding dependency among blocks can be a headache. Canceling or suspending a block creates extra work for you as a developer!

    NSOperation and NSOperationQueue add a little extra overhead compared to GCD, but you can add dependency among various operations. You can re-use, cancel or suspend operations. NSOperation is compatible with Key-Value Observation (KVO); for example, you can have an NSOperation start running by listening to NSNotificationCenter.

    For detailed explanation, refer this question: NSOperation vs Grand Central Dispatch

提交回复
热议问题