2012-03-23 47 views
3

我目前使用此代码从一个URL数据: NSURL *url = [[NSURL alloc] initWithString:urlstring]; NSString *stringfromFB = [[NSString alloc] initWithContentsOfURL:url];异步获取数据,而不是使用initWithContentsOfURL

我不知道我怎么会异步地收集这些数据,以便我的应用程序也不会冻结,每次我需要执行此操作。谢谢!

回答

0
// Do not alloc init URL obj. for local use. 
NSString *urlString = @"put your url string here"; 

NSURL *url = [NSURL URLWithString:urlString]; 

NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

[[[NSURLConnection alloc] initWithRequest:request delegate:self] autorelease]; 


- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 

上述委托方法是NSURLConnectionDelegate,你需要处理像响应误差等所有的东西。它是由提供的,所以我们可以默认直接覆盖它,而不

我有一次用在我的项目它将为异步请求工作,但如果你接收到的图像的数量,以及随后还用IconDownloaderEGOImageView它实现了图像的延迟加载,并使应用程序冻结的可能性很低。

0

如果您不想等待连接完成加载url,则可以使用NSURLConnection异步加载它。

[NSURLConnection connectionWithRequest: 
    [NSURLRequest requestWithURL: 
    [NSURL URLWithString:yourUrlString]] 
    delegate:self]; 
8

最简单的方法是提供OS5这样的:

NSString *stringfromFB; 
NSURL *url = [[NSURL alloc] initWithString:urlstring]; 
NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 
    if (data) { 
     stringfromFB = [[NSString alloc] initWithData:data 
              encoding:NSUTF8StringEncoding]; // note the retain count here. 
    } else { 
     // handle error 
    } 
}]; 

如果你被困在OS < 5出于某种原因,你需要开始一个代表您的连接,实施代表协议as illustrated here(以及其他地方的许多地方)。

+0

上面的例子在结尾处缺少a],但是除此之外真是太神奇了!谢谢! – ripegooseberry 2012-10-26 06:45:24

4

你可以用GCD做到这一点:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{ 
    NSURL *url = [[NSURL alloc] initWithString:urlstring]; 
    NSString *stringfromFB = [[NSString alloc] initWithContentsOfURL:url] 
}); 
+0

GCD会自动为您执行多线程吗?对不起,如果简单的问题,我还没有经验。谢谢!如果您使用dispatch_async和dispatch_get_global_queue,则为 – 2012-03-24 03:00:51

+0

。它是TASK PARALELISM的一个实现,非常易于使用且功能非常强大(支持多核)。你可以阅读它:http://en.wikipedia.org/wiki/Grand_Central_Dispatch和https://developer.apple.com/library/ios/#documentation/Performance/Reference/GCD_libdispatch_Ref/Reference/reference.html。永远是我在iOS平台上的首选。 – LuisEspinoza 2012-03-25 00:00:23

0

现在NSURLConnection已被弃用,您需要使用NSURLSession。

NSURLSession *session = [NSURLSession sharedSession]; 

NSURLSessionDataTask *task = [session dataTaskWithRequest: request 
             completionHandler: ^(NSData *data, NSURLResponse *response, NSError *networkError) { 

      NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)response; 

      if ([httpResponse statusCode] == STATUS_OK) { 
       // use data      
      } 
      else { 
       NSLog(@"Network Session Error: %@", [networkError localizedFailureReason]); 
      } 
}]; 

[task resume]; 
相关问题