多个异步网址请求 [英] Multiple asynchronous URL requests

查看:236
本文介绍了多个异步网址请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的iPhone应用程序通过启动屏幕时,它已经滞后或者冻结。我认为这是由于远程推送通知的注册是作为同步请求发送,因此我想将其更改为异步。
这是一个问题,因为我已经发送一个异步请求检索一些数据并将其保存到手机。
所以,我想要异步发送这两个请求,并让他们做两个不同的事情在 - (void)connectionDidFinishLoading:(NSURLConnection *)connection 。因此,我需要知道两个连接中哪一个完成了。



有什么办法吗?有没有什么方法来区分完成的连接的URL?
实际上,我认为这将像设置标签一样容易,并在 - (void)connectionDidFinishLoading:(NSURLConnection *)



有没有人知道我该怎么做?


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



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



然后,你的didReceiveData函数应该看起来像:

   - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{
if(connection == pushNotificationConnection)
{
// handle推送通知相关数据
}
else if(connection == someOtherConnection)
{
//处理其他连接
}
}


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?

解决方案

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

这篇关于多个异步网址请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆