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
DispatchQueue.main is an instance of DispatchQueue . All dispatch queues can schedule their work to be executed sync or async .
GCD is a low-level C-based API that enables very simple use of a task-based concurrency model. NSOperation and NSOperationQueue are Objective-C classes that do a similar thing. NSOperation was introduced first, but as of 10.5 and iOS 2, NSOperationQueue and friends are internally implemented using GCD .
An operation queue invokes its queued Operation objects based on their priority and readiness. After you add an operation to a queue, it remains in the queue until the operation finishes its task. You can't directly remove an operation from a queue after you add it. Note.
Overview. The BlockOperation class is a concrete subclass of Operation that manages the concurrent execution of one or more blocks. You can use this object to execute several blocks at once without having to create separate operation objects for each.
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 simple
DispatchQueue.main.async { /* do work */ }
will be just fine. That would be equivalent to
dispatch_async(dispatch_get_main_queue(), ^{ /* do work */ });
in Objective-C, which is also how I would do it in that language.
When to Use NSOperation
The NSOperation
API is great for encapsulating well-defined blocks of functionality. You could, for example, use an NSOperation
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, then Grand Central Dispatch
is a great alternative. Another benefit of Grand Central Dispatch
is that you can keep related code together. Take a look at the following example.
let dataTask = session.dataTaskWithRequest(request, completionHandler: { (data, response, error) -> Void in // Process Response ... dispatch_async(dispatch_get_main_queue(), { () -> Void in // Update User Interface ... }) })
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.
Quoted verbatim from this source
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With