Search Your Question

Difference between OperationQueue and DispatchGroup

Ans : 

You probably found yourself in a situation where you had to do a bunch of asynchronous tasks and you just wanted to get notified when all of them finish. There are two easy ways of doing this: DispatchGroup and OperationQueue. 

DispatchGroup :

DispatchGroups is probably the easiest way for you to know when a bunch of asynchronous calls is finished.  DispatchGroup is a part of GCD and in Swift 3 we got a nice swiftified GCD.

func getMovies(onCompleted: (([MovieItem]) -> ())?) {
    
    var result: [MovieItem] = []
    
    let dispatchGroup = DispatchGroup()
    
    for i in 1...5 {
        guard
            let urlString = DataSourceConstants.URLString(forPage: "\(i)")
            else {
                continue
        }
        
        dispatchGroup.enter()
        self.networkingProvider.restCall(urlString: urlString) {
            
            (responseObject) in
            
            guard
                let responseData = responseObject,
                let jsonObject = try? JSONSerialization.jsonObject(with: responseData, options: [JSONSerialization.ReadingOptions.allowFragments])
                else {
                    dispatchGroup.leave()
                    return
            }
            
            let movies = self.moviesFactory.movieItems(withJSON: jsonObject)
            result = result + movies
            
            dispatchGroup.leave()
        }
    }
    
    dispatchGroup.notify(queue: DispatchQueue.global()) {
        onCompleted?(result)
    }

}

Here we create your DispatchGroup at the top of the function and just before you execute an async piece of code, you enter the group. In your closure, you leave the same group just before the closure completes. When your DispatchGroup is empty, a closure will be called.

What’s really important here is the enter-leave pairs. You have to be very careful and make sure that you leave the group. It would be easy to introduce a bug in the code above. Let’s say that we didn’t leave the group in that guard statement above, just before the return. If the API called failed, or the JSON was malformed, the number of group entries would not match the number of leaves. So the group completion handler would never get called. If you’re calling this method from the UI and displaying an activity indicator while your networking requests are running, you would never get a callback from the method, and you would keep on spinning.

OperationQueue :

Operation queues are great and all, but if you just want to know when your queue is finished you won’t find a ready-made API waiting for you.

There is a simple trick you can use to get notified when your async tasks are finished. The trick is to use dependencies. You have two options here. If you need your operations to execute one after another, you can set the next operation to be dependent on the previous. So when your last operation is finished, your queue is finished as well. This is easy to do.

For concurrent operations(executing at same time),  Create another operation. Operations can have dependencies on multiple operations. So when you create your operations you add them as a dependency to that operation. When all dependent operation finish, your operation will get executed. And this way you can tell that your ‘queue’ is finished. If you think about this from a logical perspective, it makes perfect sense. Anyone can add a bunch of operations in the operation queue, and if you don’t own it completely, you don’t know who added what. So having a callback that will tell you that the queue is empty would not be very useful. Dependencies are baked into the Operations.

func getMovies(onCompleted: (([MovieItem]) -> ())?) {
    
    operationQueue.cancelAllOperations()
    
    var result: [MovieItem] = []
    
    let queueCompletionOperation = BlockOperation {
        onCompleted?(result)
    }
    
    var operations: [Operation] = []
    
    operationQueue.isSuspended = true
    
    for i in 1...5 {
        guard
            let urlString = DataSourceConstants.URLString(forPage: "\(i)")
            else {
                continue
        }
        
        let networkingOperation = GetDataOperation(withURLString: urlString, andNetworkingProvider: networkingProvider)
        let parsingOperation = ParseDataOperation(withFactory: moviesFactory)
        
        networkingOperation.completionBlock = {
            parsingOperation.moviesData = networkingOperation.responseData
        }
        
        parsingOperation.completionBlock = {
            if let moviesArray = parsingOperation.movies {
                DispatchQueue.global().sync(flags: .barrier) {
                    result = result + moviesArray
                }
            }
        }
        
        parsingOperation.addDependency(networkingOperation)
        
        queueCompletionOperation.addDependency(parsingOperation)
        
        operations.append(contentsOf: [parsingOperation, networkingOperation])
    }
    
    operations.append(queueCompletionOperation)
    
    operationQueue.addOperations(operations, waitUntilFinished: false)
    operationQueue.isSuspended = false
}

We have our ‘queueCompletionOperation’ at the top and in the block we’re calling our closure. We suspend the queue before adding new operations and we’re setting the dependency on our queueCompletionOperation to parsingOperation. After the loop is finished, we add all the operations in the queue and un-suspend the queue.


Dispatch queue :

DispatchQueue is an abstraction layer on top of the GCD queue that allows you to perform tasks asynchronously and concurrently in your application. Tasks are always executed in the order they’re added to the queue.
There are two types of dispatch queues :
1. Serial Dispatch Queue
2. Concurrent Dispatch Queue


No comments:

Post a Comment

Thanks