我想执行多个 Alamofire 请求。但是,由于数据依赖性,新请求只能在前一个请求完成后才开始。
我已经问了 question一个更通用的异步请求示例,该示例使用 OperationQueue
解决。但是,我没有成功地使用 Alamofire 实现同样的目标。
public func performAlamofireRequest(_ number: Int, success: @escaping (Int) -> Void)->Void {
Alamofire.request(String(format: "http://jsonplaceholder.typicode.com/posts/%i", number+1)) // NSURLSession dispatch queue
.responseString { response in // Completion handler at main dispatch queue?
if response.result.isSuccess {
// print("data")
} else if response.result.isFailure {
// print("error")
}
success(number) // Always leave closure in this example
}
}
为了确保请求在下一个请求开始之前完成,我使用 OperationQueue
如下:
let operationQueue = OperationQueue.main
for operationNumber in 0..<4 { // Create some operations
let operation = BlockOperation(block: {
performAlamofireRequest(operationNumber) { number in
print("Operation #\(number) finished")
}
})
operation.name = "Operation #\(operationNumber)"
if operationNumber > 0 {
operation.addDependency(operationQueue.operations.last!)
}
operationQueue.addOperation(operation)
}
但是,输出是:
Operation #0 finished
Operation #3 finished
Operation #2 finished
Operation #1 finished
这显然是不正确的。
如何使用 Alamofire 实现这一目标?
最佳答案
问题与 related question 中的问题相同你提出:操作依赖于完成一个操作,如记录的那样,但是你已经编写了代码,在异步调度请求以供将来执行后操作退出(你创建并添加到队列的操作将按照他们设置的顺序完成依赖项,但请求将由底层 Alamofire 的 NSURLSession 同时触发)。
如果您需要串行执行,您可以执行以下操作:
// you should create an operation queue, not use OperationQueue.main here –
// synchronous network IO that would end up waiting on main queue is a real bad idea.
let operationQueue = OperationQueue()
let timeout:TimeInterval = 30.0
for operationNumber in 0..<4 {
let operation = BlockOperation {
let s = DispatchSemaphore(value: 0)
self.performAlamofireRequest(operationNumber) { number in
// do stuff with the response.
s.signal()
}
// the timeout here is really an extra safety measure – the request itself should time out and end up firing the completion handler.
s.wait(timeout: DispatchTime(DispatchTime.now, Int64(timeout * Double(NSEC_PER_SEC))))
}
operationQueue.addOperation(operation)
}
讨论了各种其他解决方案 in connection to this question ,可以说是重复的。还有 Alamofire-Synchronous .
关于swift - 如何执行一个接一个完成的多个 Alamofire 请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39919852/