gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 13:32:27 27 4
gpt4 key购买 nike

我有一组 dispatch workItems,如何等到一个工作完成后再继续队列中的下一个工作?

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{

}
}
}

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

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

    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 ,但目前操作是可行的方法。)操作被设计为比 DispatchWorkItem 更优雅地支持异步进程的包装。因此,您可以使用 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,但您可以编写排列/子类,使用 JSONDecoder 或其他任何方式将其进一步解析为您的 Web 服务返回的任何内容。但希望这能说明基本思想。

上面使用了这个 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 我们可以将其用于任何异步过程。

关于swift - 在继续下一个请求或调度队列中的下一个 DispatchWorkItem 之前,我如何等待接收来自 DispatchWorkItem 的响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57145963/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com