【问题标题】:NSURLConnection send request after finish all processNSURLConnection 完成所有进程后发送请求
【发布时间】:2016-08-21 06:34:37
【问题描述】:

我有一个发送请求的嵌套循环。

-(void) download
{
   for(NSString *id in array)
   {
    //init with request and start the connection
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy: NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0];
    NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request deletegate:self];
    [conn start];
   }
}

-(void) connection:(NSURLConnection *) connection didReceiveData:(NSData *) data
{
//enter here secondly
}
-(void) connectionDidFinishLoading:(NSURLConnection *) connection
{
//enter here last, after finish the for loop
//my intention is use the downloaded data to do something before sending a new request.
}

问题是我想在for循环中再次发送请求之前先输入"-(void) connectionDidFinishLoading:(NSURLConnection *) connection"

但目前它将完成for循环并将进入之前的所有请求发送到"-(void) connectionDidFinishLoading:(NSURLConnection *) connection"

【问题讨论】:

  • [NSURLConnection sendSynchronousRequest:请求返回响应:响应错误:无]
  • 您可以将 NSOperationQueue 与 addDependency 或 MaxConcurrentOperation 一起使用。
  • @PKT 我认为您的解决方案对我来说已经足够了。谢谢
  • @ArunGupta tq 无论如何都要为你提供帮助。
  • 如果它适合你,让它正确,这样其他人也可以使用它......

标签: ios objective-c iphone nsurlconnection nsconnection


【解决方案1】:

你应该试试这个 NSURLConnection 在 iOS9 中被弃用

for (NSString *URL in URLArray) {
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];

NSURLSessionTask *task = [[NSURLSession sharedSession] dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // check error and/or handle response here 
}];
[task resume];
}

并使用dispatch_group_t group = dispatch_group_create();

将行添加到 for 循环 dispatch_group_enter(group); 将调用

dispatch_group_notify(group, dispatch_get_main_queue(), ^{
    // Request Finish
});

为了你的目标

【讨论】:

    【解决方案2】:

    在您的情况下,您需要尝试阻止功能,因为根据您的要求,您希望响应另一个请求的第一个连接。

    for(NSString* url in array)
    {
       // Generate a NSURLRequest object from the address of the API.
       NSURL *url = [NSURL URLWithString:urlLink];
       NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
       // Send the request asynchronous request using block!
       [NSURLConnection sendAsynchronousRequest:request
                                          queue:[NSOperationQueue mainQueue]
                              completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    
                               if (error) {
                                   NSLog(@"Error in updateInfoFromServer: %@ %@", error, [error localizedDescription]);
                               } else if (!response) {
                                   NSLog(@"Could not reach server!");
                               } else if (!data) {
                                   NSLog(@"Server did not return any data!");
                               } else {
                                   [self doStuffWithData:data];
                               }
                           }];
    }
    

    【讨论】:

      【解决方案3】:

      URL 加载不是同步操作(或者至少不应该同步完成),因为仅 DNS 查找失败就可能需要长达 90 秒,如果服务器不断运出数据则几乎无限长。如果你阻塞主线程的时间只是其中的一小部分,iOS 就会杀死你的应用程序。

      您需要安排第一个请求(并且只安排第一个请求),而不是在循环中安排请求并等待它们完成。然后,在您的 connectionDidFinishLoading: 方法(也可能是您的 connection:DidFailWithError: 方法)中,安排下一个请求。

      话虽如此,除非您仍需要支持 iOS 6/10.8 及更早版本,否则您可能应该使用 NSURLSession。 (同样的一般建议也适用;更改了委托方法名称以保护有罪者。)

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2014-07-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多