我要如何等待DispatchWorkItem的响应,然后再继续处理Dispatch队列中的下一个请求或下一个DispatchWorkItem
我有一个调度工作项数组,如何在我完成队列中的下一项工作之前等到一项工作完成?
I have an array of dispatch workItems, how to wait until one work is completed before i move on to the next work in the queue?
func AsyncCalls(statusHandler: @escaping (String) -> Void){
var dispatchWorkItems : [DispatchWorkItem] = []
let categoryWorkItem = DispatchWorkItem {
main {
return statusHandler("Loading categories ")
}
self.modelView.getCategories(completion: { data,error in
main {
if data.isEmpty {
return statusHandler("\(error )")
}else{
return statusHandler("Done loading categories")
}
}
})
}
let itemsWorkItem = DispatchWorkItem {
main {
return statusHandler("Loading Inventory ")
}
self.modelView.getInventory(completion: { data,error in
main {
if data.isEmpty {
return statusHandler("\(error )")
}else{
return statusHandler("Done loading Inventory")
}
}
})
}
dispatchWorkItems.append(categoryWorkItem)
dispatchWorkItems.append(itemsWorkItem)
let queue = DispatchQueue(label: "com.dataLoader")
let group = DispatchGroup()
dispatchWorkItems.forEach{queue.async(group: group, execute: $0)}
group.notify(queue: .main) {
main{
}
}
}
如何简化上述方法或如何应用semaphores
或任何其他可接受的方法来帮助我等到得到DispatchworkItem
的响应后再继续执行队列中的下一个DispatchworkItem
How can i simplify the above method or how do i apply semaphores
or any other accepted approach to help me wait until i get a response from a DispatchworkItem
before moving on to execute the next DispatchworkItem
in a queue
从服务器获取数据的modelView如下所示
A modelView to getData from server looks something like the below
func getInventory(completion: @escaping ArrayClosure<[InventoryClass], String>){
let parameters : [(String,AnyObject)] = [
("PageNumber" , "1" as AnyObject),
("Limit","1000" as AnyObject),
("BranchIds","\(business.branch?.id ?? "")" as AnyObject),
("canBeSold","true" as AnyObject)
]
InventoryService(authorizationHeader: self.header).getInventory(parameters: parameters) { request in
switch request {
case .success(let data):
guard let finalData = data.data else {return completion([], "Request to get Inventory Items was sucessfull but items count is 0")}
return completion([finalData],"")
case .failure(let error):
return completion([],error.localizedDescription)
}
}
}
我可能建议不要使用信号量之类的东西来阻塞线程,以便仅出于DispatchWorkItem
的原因就可以使异步任务同步运行.
I might advise against using semaphores or the like to block threads so that you can make asynchronous tasks behave synchronously, solely for the sake of DispatchWorkItem
.
当我想在异步任务之间建立依赖关系时,我一直使用Operation
而不是DispatchWorkItem
. (不可否认,在iOS 13及更高版本中,我们可能会考虑合并的未来/承诺,但目前操作仍在进行中.)操作旨在支持与DispatchWorkItem
相比,异步过程的包装要优雅得多.因此,您可以使用maxConcurrentOperationCount
为1的队列,如下所示:
When I want to establish dependencies between asynchronous tasks, I have historically used Operation
rather than DispatchWorkItem
. (Admittedly, in iOS 13 and later, we might contemplate Combine’s Future/Promise, but for now operations are the way to go.) Operations have been designed to support wrapping of asynchronous processes much more elegantly than DispatchWorkItem
. So you can use a queue whose maxConcurrentOperationCount
is 1, like so:
let networkQueue = OperationQueue()
networkQueue.maxConcurrentOperationCount = 1
let completionOperation = BlockOperation {
print("all done")
}
for url in urls {
let operation = NetworkOperation(url: url) { result in
switch result {
case .failure(let error):
...
case .success(let data):
...
}
}
completionOperation.addDependency(operation)
networkQueue.addOperation(operation)
}
OperationQueue.main.addOperation(completionOperation)
或者您可以使用更合理的maxConcurrentOperationCount
并仅在需要此顺序行为的那些操作之间使用依赖项:
Or you can use a more reasonable maxConcurrentOperationCount
and use dependencies only between those operations where you need this sequential behavior:
let networkQueue = OperationQueue()
networkQueue.maxConcurrentOperationCount = 4
let completionOperation = BlockOperation {
print("all done")
}
var previousOperation: Operation?
for url in urls {
let operation = NetworkOperation(url: url) { result in
switch result {
case .failure(let error):
...
case .success(let data):
...
}
}
if let previousOperation = previousOperation {
operation.addDependency(previousOperation)
}
completionOperation.addDependency(operation)
networkQueue.addOperation(operation)
previousOperation = operation
}
OperationQueue.main.addOperation(completionOperation)
NetworkOperation
可能是这样的:
class NetworkOperation: AsynchronousOperation {
typealias NetworkCompletion = (Result<Data, Error>) -> Void
enum NetworkError: Error {
case invalidResponse(Data, URLResponse?)
}
private var networkCompletion: NetworkCompletion?
private var task: URLSessionTask!
init(request: URLRequest, completion: @escaping NetworkCompletion) {
super.init()
task = URLSession.shared.dataTask(with: request) { data, response, error in
defer {
self.networkCompletion = nil
self.finish()
}
guard let data = data, error == nil else {
self.networkCompletion?(.failure(error!))
return
}
guard
let httpResponse = response as? HTTPURLResponse,
200..<300 ~= httpResponse.statusCode
else {
self.networkCompletion?(.failure(NetworkError.invalidResponse(data, response)))
return
}
self.networkCompletion?(.success(data))
}
networkCompletion = completion
}
convenience init(url: URL, completion: @escaping NetworkCompletion) {
self.init(request: URLRequest(url: url), completion: completion)
}
override func main() {
task.resume()
}
override func cancel() {
task.cancel()
}
}
这回传了Data
,但是您可以编写排列/子类,将其进一步解析为使用JSONDecoder
或返回的Web服务返回的任何内容.但是希望这可以说明基本思想.
This is passing back Data
, but you can write permutations/subclasses that further parse that into whatever your web service is returning using JSONDecoder
or whatever. But hopefully this illustrates the basic idea.
上面使用了这个AsynchronousOperation
类:
/// Asynchronous operation base class
///
/// This is abstract to class performs all of the necessary KVN of `isFinished` and
/// `isExecuting` for a concurrent `Operation` subclass. You can subclass this and
/// implement asynchronous operations. All you must do is:
///
/// - override `main()` with the tasks that initiate the asynchronous task;
///
/// - call `completeOperation()` function when the asynchronous task is done;
///
/// - optionally, periodically check `self.cancelled` status, performing any clean-up
/// necessary and then ensuring that `finish()` is called; or
/// override `cancel` method, calling `super.cancel()` and then cleaning-up
/// and ensuring `finish()` is called.
public class AsynchronousOperation: Operation {
/// State for this operation.
@objc private enum OperationState: Int {
case ready
case executing
case finished
}
/// Concurrent queue for synchronizing access to `state`.
private let stateQueue = DispatchQueue(label: Bundle.main.bundleIdentifier! + ".rw.state", attributes: .concurrent)
/// Private backing stored property for `state`.
private var _state: OperationState = .ready
/// The state of the operation
@objc private dynamic var state: OperationState {
get { stateQueue.sync { _state } }
set { stateQueue.sync(flags: .barrier) { _state = newValue } }
}
// MARK: - Various `Operation` properties
open override var isReady: Bool { return state == .ready && super.isReady }
public final override var isAsynchronous: Bool { return true }
public final override var isExecuting: Bool { return state == .executing }
public final override var isFinished: Bool { return state == .finished }
// KVN for dependent properties
open override class func keyPathsForValuesAffectingValue(forKey key: String) -> Set<String> {
if ["isReady", "isFinished", "isExecuting"].contains(key) {
return [#keyPath(state)]
}
return super.keyPathsForValuesAffectingValue(forKey: key)
}
// Start
public final override func start() {
if isCancelled {
state = .finished
return
}
state = .executing
main()
}
/// Subclasses must implement this to perform their work and they must not call `super`. The default implementation of this function throws an exception.
open override func main() {
fatalError("Subclasses must implement `main`.")
}
/// Call this function to finish an operation that is currently executing
public final func finish() {
if isExecuting { state = .finished }
}
}
有很多写基础AsynchronousOperation
的方法,我不想在细节上迷失方向,但是想法是我们现在有了一个Operation
,我们可以将其用于任何异步过程.
There are lots of ways to write a base AsynchronousOperation
, and I don’t want to get lost in the details, but the idea is that we now have an Operation
that we can use for any asynchronous process.