如何执行接连完成的多个Alamofire请求?

编程入门 行业动态 更新时间:2024-10-11 17:23:45
本文介绍了如何执行接连完成的多个Alamofire请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想执行多个Alamofire请求。但是,由于数据依赖性,新请求仅应在前一个请求完成时才开始。

I would like to perform multiple Alamofire requests. However, because of data dependency a new request should only start when the previous is finished.

我已经问过,其中有一个更一般的异步请求示例,该示例由 OperationQueue 解决。但是,我无法通过Alamofire实现相同的功能。

I already asked a question with a more general example of an asynchronous request which was solved with OperationQueue. However, I do not succeed to achieve the same with Alamofire.

public func performAlamofireRequest(_ number: Int, success: @escaping (Int) -> Void)->Void { Alamofire.request(String(format: "jsonplaceholder.typicode/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 如下:

To assure that requests are finished before a next request is started, I use OperationQueue as follows:

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做到这一点?

How would it be possible to achieve this with Alamofire?

推荐答案

问题与您提出的相关问题:如文档所述,操作依赖项是对操作的完成,但是您已经编写了代码,其中该操作在异步调度将来执行的请求后退出操作(您创建并添加到的操作队列将按照其依赖关系设置的顺序完成,但请求将由Alamofire底层的NSURLSession并发触发。

The issue is just the same as in the related question you posed: the operation dependencies are on finishing an operation, as documented, but you have written code where the operation exits after asynchronously dispatching a request for future execution (the operations you created and added to a queue will finish in the order set by their dependencies, but the requests will be fired concurrently by the NSURLSession underlying Alamofire).

如果需要串行执行,则可以执行以下操作:

If you need serial execution, you can for instance do the following:

// 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) }

讨论了各种其他解决方案 ,可以说是重复的。还有 Alamofire-Synchronous 。

Various other solutions are discussed in connection to this question, arguably a duplicate. There's also Alamofire-Synchronous.

更多推荐

如何执行接连完成的多个Alamofire请求?

本文发布于:2023-11-26 02:03:53,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1632212.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   Alamofire

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!