OperationQueue.main vs DispatchQueue.main

后端 未结 3 842
执笔经年
执笔经年 2021-02-01 15:33

When you need to perform something on the main thread in the completion block of a networking task or an operation, which of these ways to get it would be the most appropriate a

3条回答
  •  暖寄归人
    2021-02-01 15:40

    DispatchQueue manages the execution of work items. Each work item submitted to a queue is processed on a pool of threads managed by the system.

    Reference : Apple Doc

    The NSOperationQueue class regulates the execution of a set of Operation objects. After being added to a queue, an operation remains in that queue until it is explicitly canceled or finishes executing its task. Operations within the queue (but not yet executing) are themselves organized according to priority levels and inter-operation object dependencies and are executed accordingly. An application may create multiple operation queues and submit operations to any of them.

    Reference : Apple Doc

    So, you should prefer DispatchQueue.main.async when you want to perform something on main thread from completion block of any network call. Especially when it is related to UI Updates! And If your task is complex, I mean if you require further operation on running task then you can go with OperationQueue.main.addOperation otherwise DispatchQueue.main.async will give more optimal performance comparatively!

提交回复
热议问题