ASIHTTPRequest 中的请求顺序
我正在使用 ASIHTTPRequest 下载一些数据。
我的方法中有以下内容:
// Request 1
request1.tag = 1;
[request1 setDelegate:self];
[request startAsynchronous];
// Request 2
request2.tag = 2;
[request2 setDelegate:self];
[request2 startAsynchronous];
// Call third request method
[self callThirdRequest];
现在,从 callThirdRequest
中,我正在获取从 request2
下载的一些数据,并在其中调用 startAsynchronous< /代码>。我在单独的方法中调用第三个请求的原因是因为它会被多次调用。在放置一些控制台输出后,似乎在
request2
开始下载之前调用了 callThirdRequest
。因此,当callThirdRequest
尝试获取一些本应由request2
下载的数据时,它不起作用,因为没有数据。
这是为什么?如何确保仅在 request2
下载完成时调用 callThirdRequest
?
谢谢,
I am using ASIHTTPRequest to download some data.
I have the following in a method:
// Request 1
request1.tag = 1;
[request1 setDelegate:self];
[request startAsynchronous];
// Request 2
request2.tag = 2;
[request2 setDelegate:self];
[request2 startAsynchronous];
// Call third request method
[self callThirdRequest];
Now from within callThirdRequest
, I am grabbing some data that has been downloaded from request2
and in there, I am calling startAsynchronous
. The reason I have the calling of the third request in a separate method is because it will get invoked more than once. After putting some console outputs, it seems that callThirdRequest
is being invoked before request2
starts its download. Therefore, when callThirdRequest
tries to grab some data which should have been downloaded by request2
, it does not work as there is no data.
Why is that? How can I make sure callThirdRequest
is only called when request2
is done downloading?
Thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
当请求异步运行时,其余代码将同时继续运行。您的前两个请求正是这样做的。你有两个选择。
您可以同步运行第二个请求。 (这不是一个好主意,因为您的应用程序将显示为“冻结”,直到第二个请求完成。此外,如果您的三个请求列表中的第二个请求失败,则使用此方法不会对您有帮助。)
更好的主意是使用委托回调方法。这是处理这个问题的更好方法,原因有两个。首先,你可以正确处理失败的请求,同样,你也可以正确处理成功的请求。 Yu 想要这样的事情:
确保检查委托回调中的请求,以确保它是“第二个”请求,这样您就不会最终在错误的时间触发错误的操作。在本例中,我使用了请求的“tag”属性。如果您希望保留该请求作为您班级的财产,您也可以检查一下。
When a request is run asynchronously, the rest of your code will continue to run at the same time. Your first two requests are doing exactly that. You have two options.
You can run the second request synchronously. (Not a good idea, since your app will appear "frozen" until the second request completes. Also, using this method won't help you if the second request, in your list of three, fails.)
A much better idea would be to use delegate callback methods. This is the better way of dealing with this, for two reasons. First of all, you can handle failed requests properly, and also, you can handle successful requests properly. Yu want something like this:
Make sure to check the request in the delegate callback, to ensure that it's the "second" one, so that you don't end up firing the wrong action at the wrong time. In this case, I used the "tag" property of the request. If you would have retained the request as a property of your class, you can just check for that as well.
这是因为 request2 正在异步运行。您应该在 request2 的 request-did-finish-delegate-method 中启动第三个请求!
That is because request2 is running asynchronous. You should start the 3rd request in the request-did-finish-delegate-method of request2!
原因是
startAsynchronous
。异步调用将在另一个线程上操作。您需要在 request2 上设置委托,然后在 request2 完成时调用第三个请求。The reason is
startAsynchronous
. An asynchronous call will operate on another thread. You need to set the delegate on request2 and then call the third request when request2 is finished.