多个异步 URL 请求

发布于 2024-10-31 04:11:08 字数 449 浏览 3 评论 0原文

我的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

末骤雨初歇 2024-11-07 04:11:08

正如 Kalle 所说,最好的办法是使用一个类来处理连接、解析响应并在漂亮的委托函数中返回数据。

然而,如果由于某种原因你必须使用同一个委托创建 2 个 NSURLConnections,你想要做的就是将对它们的引用保存在类 ivars 中。类似于 NSURLConnection *pushNotificationConnection;和 NSURLConnection *someOtherConnection;

然后,您的 didReceiveData 函数应类似于:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (connection == pushNotificationConnection)
    {
        // handle the push notification related data
    }
    else if (connection == someOtherConnection)
    {
        // handle the other connection
    }
}

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:

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    if (connection == pushNotificationConnection)
    {
        // handle the push notification related data
    }
    else if (connection == someOtherConnection)
    {
        // handle the other connection
    }
}
花开半夏魅人心 2024-11-07 04:11:08

执行此操作的一种简洁方法是实际上让一个单独的类处理每个请求。或者更确切地说,您有一个应该执行请求、获取数据的类,然后在完成后将它们发送回(通过委托)到主类。

因此,您将有两个类,例如 PushNotificationRequestorSomeLoader。每个都将创建和维护自己单独的 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 and SomeLoader. Each would create and maintain their own separate HTTP (or whatever type it is) requests and would each have their own separate connectionDidFinishLoading: etc. methods.

强辩 2024-11-07 04:11:08
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  if ([connection isEquals:pushNotificationConnection]) {
    // handle the push notification related data
  } else if ([connection isEquals:someOtherConnection]) {
    // handle the other connection
  }
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
  if ([connection isEquals:pushNotificationConnection]) {
    // handle the push notification related data
  } else if ([connection isEquals:someOtherConnection]) {
    // handle the other connection
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文