【问题标题】:Asynchronously getting data instead of using initWithContentsOfURL异步获取数据而不是使用 initWithContentsOfURL
【发布时间】:2012-04-08 12:33:12
【问题描述】:

我目前正在使用此代码从 URL 获取数据: NSURL *url = [[NSURL alloc] initWithString:urlstring]; NSString *stringfromFB = [[NSString alloc] initWithContentsOfURL:url];

我想知道如何异步收集这些数据,这样我的应用程序就不会在每次需要执行时都冻结。谢谢!

【问题讨论】:

    标签: iphone asynchronous nsstring nsurl


    【解决方案1】:

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

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

    【讨论】:

      【解决方案2】:

      在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
          }
      }];
      

      如果您由于某种原因被困在 osas illustrated here(以及其他许多地方)。

      【讨论】:

      • 上面的例子最后缺少一个 ] ,但除此之外真是太棒了!谢谢!
      【解决方案3】:

      你可以用 GCD 做到这一点:

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

      【讨论】:

      【解决方案4】:
      // 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图像和冻结应用程序的机会非常低。

      【讨论】:

        【解决方案5】:

        现在 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];
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多