多个异步 URL 请求
我的 iPhone 应用程序在经过启动屏幕时一直“滞后”或者更确切地说“冻结”。我认为这是由于远程推送通知的注册是作为同步请求发送的,因此我想将其更改为异步。 这是一个问题,因为我已经发送了一个异步请求来检索一些数据并将其保存到手机。 因此,我想异步发送这两个请求,并让它们在 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
中执行两个不同的操作。因此我需要知道两个连接中哪一个已完成。
有什么办法可以做到这一点吗?有什么方法可以通过完成连接的URL来区分吗? 实际上,我认为这就像设置一个 tag
并在 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
中检查它一样简单,但这似乎不可能。
有谁知道我该怎么做?
My iPhone application has been "lagging" or rather "frozen" when it got past the start up screen. I think this is due to the registration for remote push notifications is send as a synchronous request and therefore I would like to change this to asynchronous.
This is a problem since I am already sending one asynchronous request for retrieving some data and save it to the phone.
So, I would like to send both of these requests asynchronously and have them do two different things in - (void)connectionDidFinishLoading:(NSURLConnection *)connection
. Therefore I need to know which of the two connections that finished.
Is there any way to do this? Would there be any way to distinguish by the URL of the finished connection?
Actually, I thought it would be as easy as set a tag
and check this in - (void)connectionDidFinishLoading:(NSURLConnection *)connection
but this does not seem to be possible.
Does anyone know how I can do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如 Kalle 所说,最好的办法是使用一个类来处理连接、解析响应并在漂亮的委托函数中返回数据。
然而,如果由于某种原因你必须使用同一个委托创建 2 个 NSURLConnections,你想要做的就是将对它们的引用保存在类 ivars 中。类似于 NSURLConnection *pushNotificationConnection;和 NSURLConnection *someOtherConnection;
然后,您的 didReceiveData 函数应类似于:
As Kalle said, the best thing to do is a class that handles the connection, parses the response, and returns the data in a pretty delegate function.
However if you must for some reason make 2 NSURLConnections with the same delegate, what you want to do is save references to them both in class ivars. Something like NSURLConnection *pushNotificationConnection; and NSURLConnection *someOtherConnection;
Then, your didReceiveData function should look something like:
执行此操作的一种简洁方法是实际上让一个单独的类处理每个请求。或者更确切地说,您有一个应该执行请求、获取数据的类,然后在完成后将它们发送回(通过委托)到主类。
因此,您将有两个类,例如
PushNotificationRequestor
和SomeLoader
。每个都将创建和维护自己单独的 HTTP(或任何类型)请求,并且每个请求都有自己单独的connectionDidFinishLoading:
等方法。A clean way to do this is to actually have a separate class handle each request. Or rather, you have a class which is supposed to perform the request, get the data, then send them back (via delegation) to the main class once it's done.
You would thus have two classes, e.g.
PushNotificationRequestor
andSomeLoader
. Each would create and maintain their own separate HTTP (or whatever type it is) requests and would each have their own separateconnectionDidFinishLoading:
etc. methods.