Skip to content

Instantly share code, notes, and snippets.

@codecat15
Last active March 28, 2021 15:21
Show Gist options
  • Save codecat15/d7ba8713e1631d27d588e808bf00b215 to your computer and use it in GitHub Desktop.
Save codecat15/d7ba8713e1631d27d588e808bf00b215 to your computer and use it in GitHub Desktop.
using combination of dispatch group and operation queue to wait for URLSession
// let's say the project operation needs to be executed first before the employee
let group = DispatchGroup()
// employee block operation
let employeeBlockOperation = BlockOperation()
employeeBlockOperation.addExecutionBlock {
let employeeDataResource = EmployeeDataResource()
employeeDataResource.getEmployee { (employees) in
//do something with the employee response
}
}
// project block operation
let projectBlockOperation = BlockOperation()
projectBlockOperation.addExecutionBlock {
// enter the dispatch group
group.enter()
let projectResource = ProjectDataResource()
projectResource.getProject { (projectData) in
// do something with the project response
// leaving the block
group.leave()
}
// we need to wait for an entire execution block to complete
group.wait()
}
// since we want the project operation to kick off first we add dependency
employeeBlockOperation.addDependency(projectBlockOperation)
// adding operations to the operation queue
let operationQueue = OperationQueue()
operationQueue.addOperation(employeeBlockOperation)
operationQueue.addOperation(projectBlockOperation)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment