ios - 使用 asynchImageView 调用网络服务

标签 ios asynchronous nsurlconnection grand-central-dispatch

当使用 AsynchImageView 文件将图像加载到 tableView 中时,我在调用 Web 服务时遇到问题。

下面是我的问题的步骤:

  1. 我调用网络服务,当它返回数据时,我重新加载 UITableView 并使用 AsynchImageView 加载所有图像。 Web 服务返回图像的 url 和一些文本数据。
  2. 在加载图像时,如果我再次调用同一个 Web 服务,它会运行 30 秒,然后超时,不会返回任何内容,但在那之后,无论我何时调用它,它都可以正常工作。

这是我调用服务的代码:

-(void)getUserNotificationsPage:(int)page CallBack:(getNotifications)callback{
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^ {
       @try {

            getNotificationsArrayResponseCallback=callback;


            NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@?token=%@&page=%@",GET_USER_NOTIFICATIONS,[[NSUserDefaults standardUserDefaults]objectForKey:@"token"],[NSString stringWithFormat:@"%i",page]]]];


            NSError *err;
            NSData *returnData = [NSURLConnection sendSynchronousRequest:urlRequest returningResponse:nil error:&err];
            if(err){
                returnData=[NSMutableData data];
            }

            NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];


   dispatch_async(dispatch_get_main_queue(), ^ {

 id json = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:nil];

        NSArray *returnArray=[json objectForKey:@"notifications"];

                getNotificationsArrayResponseCallback(returnArray,YES);

            });

        }
        @catch (NSException *exception) {


            dispatch_async(dispatch_get_main_queue(), ^ {

                UIAlertView *alert=[[UIAlertView alloc]initWithTitle:LANGUAGE(@"Unknown error occurred") message:nil delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
                [alert show];


                getNotificationsArrayResponseCallback(nil,NO);

            });
        }        
     });
}
  1. 如果我删除了使用 asynchImageview 加载图像的代码,那么我可以随时调用 Web 服务,而且响应速度很快。

    AsynchImageView *userProfileImageView =[[AsynchImageView alloc] initWithFrameURLStringAndTag:CGRectMake(5, 215, 70, 70) :[NSString stringWithFormat:@"%@%@",SERVER_URL,"some url" ];
    
    [userProfileImageView setBackgroundColor:[UIColor clearColor]];
    
    [userProfileImageView loadImageFromNetwork];
    
    [cell addSubview:userProfileImageView];
    

如你所见,如果我注释掉这一行

[userProfileImageView loadImageFromNetwork];

然后我可以多次调用 Web 服务并且响应很快,但是当异步 ImageView 正在加载图像然后我调用该服务时它只会超时。对于进一步的调用服务工作正常。

我认为这是线程化或同时调用多个 url 请求的问题。

最佳答案

这是您正在使用的类(class)吗? AsynchImageView

如果是这样,那么看起来他的 NSURLConnection 实际上并没有在后台处理。看起来它在主线程上。

我过去曾成功使用过这个库 AsyncImageView

如果您查看这个其他库,他们会使用适当的调度队列来运行后台。另一个类(class)没有。

dispatch_async(dispatch_get_main_queue(), ^(void) {

关于ios - 使用 asynchImageView 调用网络服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25889211/

相关文章:

spring - 使用 Spring + Jersey 的异步 API

ios - 在 iOS 中重试 HTTP 请求

通过 iPhone 连接到服务器时出现 SSL 错误

ios - 自定义绘制时何时更改 UIView 的框架大小?

javascript - Angular 异步管道卡在 promise 上

iOS 如何使用谓词或 KVC 将对象的多个属性转储到字典中?

ios - 如何在从主函数返回值之前等待委托(delegate)函数完成

swift - NSURLErrorDomain 代码 = -1002 尽管转义了受限字符

android - 如何使用 Ionic 2 重新加载页面

ios - iOS 6.0 ARC下需要释放GCD队列吗?