在某些刷新请求中,NSURLConnection sendAsynchronousRequest不命中完成处理程序



我有这样的代码块:

    +(void)requestPath:(NSString *)path onCompletion:(RequestCompletionHandler)complete
{
    // Background Queue
    NSOperationQueue *backgroundQueue = [[NSOperationQueue alloc] init];
    // URL Request
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:[NSURL URLWithString:path]
                                                  cachePolicy:NSURLCacheStorageAllowedInMemoryOnly
                                              timeoutInterval:10];
    // Send Request
    [NSURLConnection sendAsynchronousRequest:request
                                       queue:backgroundQueue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               if (complete) complete(result,error);
                           }];

}

这段代码可以为我的应用程序提取数据,99%的情况下我使用它都没有问题。然而,有时(但并非总是)在UIRefreshControl刷新请求中,NSURLConnection sendAsynchronousRequest中的完成处理程序没有命中,这导致我的应用程序崩溃。

我可以请求相同的数据,有时完成处理程序被击中,但其他时候刷新它将不工作。

我不知道为什么会发生这种情况。

任何帮助都将非常感激。

谢谢!

正确处理错误情况

 [NSURLConnection sendAsynchronousRequest:request
                                       queue:backgroundQueue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error){
                               if (error)
                               {
                                   NSLog(@"%@",[error localizedDescription]);
                               }
                               else
                               {
                                   NSString *result = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                   //if (complete) complete(result,error);
                               }
                           }];

相关内容

最新更新