Operation Queue vs Dispatch Queue for iOS Application

前端 未结 3 1256
天命终不由人
天命终不由人 2020-12-13 05:48
  1. What are the differences between Operation Queue and Dispatch Queue?
  2. Under what circumstances will it be more appropriate to use each?
3条回答
  •  再見小時候
    2020-12-13 06:33

    • 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 operations, cancel or suspend them. NSOperation is compatible with Key-Value Observation (KVO); for example, you can have an NSOperation start running by listening to NSNotificationCenter.

    NSOperation and NSOperationQueue are higher lever APIs, made on top of the GDC itself, to achieve the concurrency in an object oriented way.

    For detailed explanation, refer this question: https://stackoverflow.com/questions/10373331/nsoperation-vs-grand-central-dispatch

提交回复
热议问题