nsurlsession用于循环中的多个请求 [英] nsurlsession for multiple request in loop

查看:227
本文介绍了nsurlsession用于循环中的多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试下载图片/文本,但我需要这样做,直到该网址存在&下载该图像/txt文件,所以我继续再次调用相同的方法&当我设置调试点时,我会看到此.

I have been trying to download image/txt but i need to do it until that url exists & download that image/txt file,so i keep calling same method again & when i set debug point i see this .

如果url正确,则我在调试导航器中看不到任何队列,因为它没有再次调用方法.我将AFNetworking库指向相同的库,但是我想它的工作方式与我在NSURLSession中执行的方法相同,对吗?

If url is right than i do not see any queue in debug navigator because it is not calling method again. i referred AFNetworking library to the same but i guess it`s working in same way as i am doing in NSURLSession,right?

案例:-我检查网址是否存在,因此如果存在该网址,则先加载两个网址(time.txt&image.png),否则调用WebService(XmlParser)&继续检查以下文件的网址.

Case:- I check for url if exists or not, so if it`s exists than load both urls(time.txt & image.png), otherwise call WebService(XmlParser) & keep check for urls for following files.


time.txt+image.png 

or 

tryagain.txt

显示存在的任何一个.

还检查了此 AFNetworking问题,但它没有帮助因为我不想增加操作数.我想加载任何存在的文件.

Also checked this AFNetworking Question but it didnt helped because i do not want to add number of operations. i want to load file whichever exists.

因为无论AFNetworking/NSURLSession是成功还是失败,操作都将完成.

Because Operations will be completed whether it is success or fail in AFNetworking/NSURLSession.


-(void)downloading
{
 NSString *imageUrl = [NSString stringWithFormat:@"%@",txtNumber.text];
       
 NSURLSessionConfiguration *sessionConfig =[NSURLSessionConfiguration defaultSessionConfiguration];
        
 NSURLSession *session =[NSURLSession sessionWithConfiguration:sessionConfig
                                               delegate:self
                                          delegateQueue:nil];
        
 NSURLSessionDownloadTask *getImageTask = [session downloadTaskWithURL:[NSURL URLWithString:imageUrl]
                        
                                  completionHandler:^(NSURL *location,
                                                      NSURLResponse *response,
                                                      NSError *error)
                        {
                           UIImage *downloadedImage =[UIImage imageWithData:[NSData dataWithContentsOfURL:location]];

                                      dispatch_async(dispatch_get_main_queue(), ^{
                                          // do stuff with image
                                          if (downloadedImage)
                                          {
                                              carImgView.image = downloadedImage;
                                              result = TRUE;
                                          }
                                          else
                                          {
                                              result = FALSE;
                                              [self tryagain];
                                          }
                                      });
                                  }];
        
        [getImageTask resume];
}



-(void)tryagain
{
    NSString *strImg = [[NSString stringWithFormat:@"%@",gblPolicyNo] stringByAppendingString:FilePolStatus];
    
    NSString *apiImage = [NSString stringWithFormat:@"http://moviepoint.info/%@/%@",FolderPolStatus,strImg];
    
    NSURL *aImgUrl = [NSURL URLWithString:apiImage];
    // 2
    NSURLSessionConfiguration *sessionConfig =
    [NSURLSessionConfiguration defaultSessionConfiguration];
    
    // 3
    tryAgainSession =[NSURLSession sessionWithConfiguration:sessionConfig
                                           delegate:self
                                      delegateQueue:nil];
    
    
    // 1
    getTryAgainTask = [tryAgainSession downloadTaskWithURL:aImgUrl
                    
                    
                              completionHandler:^(NSURL *location,
                                                  NSURLResponse *response,
                                                  NSError *error)
                    {
                        
                        // 2
                        UIImage *downloadedImage =[UIImage imageWithData:[NSData dataWithContentsOfURL:location]];
                        //3
                        dispatch_async(dispatch_get_main_queue(), ^{
                            // do stuff with image
                            if (downloadedImage)
                            {
                                [policyImgWebView loadData:[NSData dataWithContentsOfURL:location] MIMEType:nil textEncodingName:nil baseURL:nil];
                                NSLog(@"YES");
                            }
                            else
                            {
                                NSLog(@"NO");
                                [self performInBackground];
                            }
                        });
                    }];
    
    // 4
    [getTryAgainTask resume];
    
}

如果我做错了,请纠正我&帮我解决这个问题.

Please Correct me if i am doing wrong & Help me to solve this problem.

推荐答案

通过使用一个全局NSURLSession来解决

Solved by taking One Global NSURLSession

这篇关于nsurlsession用于循环中的多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆