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 and why?:
OperationQueue.main.addOperation
DispatchQueue.main.async
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 and why?:
OperationQueue.main.addOperation
DispatchQueue.main.async
Reference : Apple Doc
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 toUI Updates
! And If your task is complex, I mean if you require further operation on running task then you can go withOperationQueue.main.addOperation
otherwiseDispatchQueue.main.async
will give more optimal performance comparatively!When to Use NSOperation
The
NSOperation
API is great for encapsulating well-defined blocks of functionality. You could, for example, use anNSOperation
subclass to encapsulate the login sequence of an application.Dependency management is the icing on the cake. An operation can have dependencies to other operations and that is a powerful feature Grand Central Dispatch lacks. If you need to perform several tasks in a specific order, then operations are a good solution.
You can go overboard with operations if you are creating dozens of operations in a short timeframe. This can lead to performance problems due to the overhead inherent to the
NSOperation API
.When to Use Grand Central Dispatch
Grand Central Dispatch
is ideal if you just need to dispatch a block of code to a serial or concurrent queue.If you don’t want to go through the hassle of creating an
NSOperation subclass
for a trivial task, thenGrand Central Dispatch
is a great alternative. Another benefit ofGrand Central Dispatch
is that you can keep related code together. Take a look at the following example.In the completion handler of the data task, we process the response and update the user interface by dispatching a closure (or block) to the main queue. This is necessary because we don’t know which thread the completion handler is executed on and it most likely is a background thread.
For details on the differences between the two types of queue, see Lion's answer.
Both approaches will work. However,
NSOperation
is mostly needed when more advanced scheduling (including dependencies, canceling, etc.) is required. So in this case, a simplewill be just fine. That would be equivalent to
in Objective-C, which is also how I would do it in that language.