首页 文章

nsurlsession for循环中的多个请求

提问于
浏览
1

我一直在尝试下载image / txt,但我需要这样做,直到该url存在并下载该image / txt文件,所以我再次调用相同的方法,当我设置调试点时,我看到
Thread Queues
.

如果url是正确的,我在调试导航器中看不到任何队列,因为它没有再次调用方法 . 我把AFNetworking图书馆推到了同样的地方,但我猜它的工作方式与我在NSURLSession中的工作方式相同,对吧?

案例: - 我检查了url是否存在,所以如果它存在而不是加载两个url(time.txt&image.png),否则调用WebService(XmlParser)并继续检查以下文件的URL .

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];

}

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

1 回答

  • 1

    采取One Global NSURLSession解决

相关问题