gpt4 book ai didi

iphone - 使用 NSURLConnection 发送一系列 HTTP 请求的最佳方式

转载 作者:可可西里 更新时间:2023-11-01 03:49:53 27 4
gpt4 key购买 nike

使用 NSURLConnection 发出的 HTTP 请求是事件驱动的。当您需要一个接一个地发出三个请求时,这会让事情变得有点奇怪,其中每个请求都使用前一个请求返回的信息。

我习惯这样做:

response1 = request1();
response2 = request2(response1);
response3 = request3(response2);

但我能找到如何使用 NSURLConnection 执行此操作的唯一方法是让 connectionDidFinishLoading: 发出下一个请求。但是当顺序请求的数量增加时,这会变得困惑。

用 Cocoa 处理顺序 HTTP 请求的惯用方法是什么?

最佳答案

您可以将请求包装在 NSOperation 中,然后定义操作依赖项,以便每个请求在执行之前必须等待其依赖的请求。

来自Apple Docs :

Dependencies are a convenient way to execute operations in a specific order. You can add and remove dependencies for an operation using the addDependency: and removeDependency: methods. By default, an operation object that has dependencies is not considered ready until all of its dependent operation objects have finished executing. Once the last dependent operation finishes, however, the operation object becomes ready and able to execute.

关于iphone - 使用 NSURLConnection 发送一系列 HTTP 请求的最佳方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10555482/

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