在继续下一个请求或调度队列中的下一个 DispatchWorkItem 之前,我如何等待接收来自 DispatchWorkItem 的响应

How can i wait to receive a response from a DispatchWorkItem before moving on to the next request or next DispatchWorkItem in a Dispatch 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: [=10=])}

    group.notify(queue: .main) {
        main{

        }
    }
}

我如何简化上述方法或如何应用 semaphores 或任何其他可接受的方法来帮助我等到 DispatchworkItem 的响应,然后再继续执行下一个DispatchworkItem 在队列中

从服务器获取数据的模型视图如下所示

    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

当我想建立异步任务之间的依赖关系时,我一直使用Operation而不是DispatchWorkItem。 (诚​​然,在 iOS 13 及更高版本中,我们可能会考虑 Combine 的 Future/Promise,但目前操作是可行的方法。)操作被设计为比 [= 更优雅地支持异步进程的包装14=]。所以你可以使用一个 maxConcurrentOperationCount 为 1 的队列,像这样:

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 并仅在需要此顺序行为的那些操作之间使用依赖关系:

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,但您可以编写 permutations/subclasses 进一步解析为使用 JSONDecoder 或其他任何方式返回的任何 Web 服务。但希望这能说明基本思想。

上面用这个AsynchronousOperation class:

/// 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 我们可以用于任何异步进程。