iPhone:与NSURL请求异步下载数据,而不是[NSData datawithContents ofURL] [英] iPhone: Downloading data asynchronously with NSURL request as opposed to [NSData datawithContents ofURL]

查看:657
本文介绍了iPhone:与NSURL请求异步下载数据,而不是[NSData datawithContents ofURL]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新手,我试图从googleapi网页下载地理编码数据。我用它做了:代码:

Hi Im a newbie and Im trying to download geocoding data from googleapi webpage. I did it using this: code:

        NSMutableString *urlStr = [[NSMutableString alloc] initWithString:temp];
        NSMutableString *address = [[NSMutableString alloc] initWithString:l.Address];
        [address appendString:@" "];
        [address appendString:l.CityName];
        [address appendString:@" "];
        [address appendString:l.State];
        [address appendString:@" "];
        [address appendString:l.PostalCode];
        NSArray *addressArray = [address componentsSeparatedByString:@" "];
        [address release];
        NSString *a = [addressArray componentsJoinedByString:@"+"];
        [urlStr appendString:a];
        [urlStr appendString:@"&sensor=false"];
        NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        NSData *data = [NSData dataWithContentsOfURL:url];

它工作正常,但故障是它锁定了我的GUI,因为它全部是同步完成的。

It worked fine but the glitch was that it locked up my GUI since it was all done synchronously.

有人建议我使用NSURLrequest异步进行。所以我去了苹果网站。从那里我看到了他们的榜样。但是,我仍然坚持。如何将上述同步请求代码转换为异步请求?到目前为止,这就是我所拥有的,但我不确定这是否有效......有点困惑......有人可以用这个代码向我指出方向吗?

Someone suggested I do it asynchronously using NSURLrequest. So I went to the apple website. from there I saw their example. However, Im still stuck. How do I convert the above code which is a synchronous request to an asynchronous one? So far this is what I have, but Im not sure if this works...... a bit confused ... could someone please point me in the righ direction with this code?

        NSURL *url = [[NSURL alloc] initWithString:[urlStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
        //create NSURL request for asynchronous processing
        NSURLRequest *theRequest = [NSURLRequest requestWithURL:url
                                                  cachePolicy:NSURLRequestUseProtocolCachePolicy
                                              timeoutInterval:60.0];

        NSURLConnection *theConnection= [[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

        if (theConnection) 
           {
            receivedData = [[NSMutableData data] retain];
           }
        else
           {
            // Inform the user that the connection failed.
           }


推荐答案

使用NSURLConnection

根据您构建代码的方式,您需要在成功完成后执行或通知其他对象(委托,通知) (connectionDidFinishLoading :)或失败(connection:didFailWithError :)。

Depending on how you structure your code, you'll want to act or notify some other object (delegate, notification) on successful completion (connectionDidFinishLoading:) or failure (connection: didFailWithError:).

这篇关于iPhone:与NSURL请求异步下载数据,而不是[NSData datawithContents ofURL]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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