1

我一直在嘗試下載圖像/ TXT,但我需要這樣做,直到該URL存在&下載該圖像/ TXT文件,所以我一直調用同樣的方法&當我設置調試點我看到這個​​。nsurlsession在循環中的多個請求

如果url比我在調試導航器中看不到任何隊列,因爲它不是再次調用方法。我將AFNetworking庫引用到了相同的地方,但我想它的工作方式與我在NSURLSession中的工作方式相同,對不對?

案例: - 我檢查URL如果網址下面的文件存在與否,所以如果它單曲存在比加載兩個URL(TIME.TXT & image.png),否則調用WebService的(XmlParser的)&保持check 。

 
time.txt+image.png 

or 

tryagain.txt 

顯示哪個存在。

也檢查了這個AFNetworking Question但它沒有幫助,因爲我不想添加操作的數量。我想加載文件,無論哪裏存在。

因爲操作將在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]; 

} 

請糾正我,如果我做錯了&幫我解決這個問題。

+0

任何人都可以幫助我解決這個問題嗎? –

回答

1

通過服用One Global NSURLSession來解決