nsurlsession for multiple request in loop

北慕城南 提交于 2020-01-05 08:42:09

问题


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

.

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?

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

show whichever exists.

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.

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.


回答1:


Solved by taking One Global NSURLSession



来源:https://stackoverflow.com/questions/22139445/nsurlsession-for-multiple-request-in-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!