循环中的多个请求的nsurlsession
我一直在试图下载图像/ TXT,但我需要做的,直到该url存在和下载该图像/ TXT文件,所以我一直调用同样的方法再次&当我设置debugging点,我看到这个 。
如果url是正确的比我没有看到在debugging导航器中的任何队列,因为它不是再次调用方法。 我把AFNetworking库引用到了相同的,但我想它的工作原理与我在NSURLSession中的工作方式一样,对吧?
案例: – 我检查url是否存在,所以如果它的存在比加载两个urls(time.txt&image.png),否则调用WebService(XmlParser)&保持检查URL以下文件。
TIME.TXT + image.png 要么 tryagain.txt
显示任何一个存在。
还检查了这个AFNetworking问题,但没有帮助,因为我不想添加操作的数量。 我想加载文件,无论哪里存在。
因为操作将在AFNetworking / NSURLSession中完成,无论是成功还是失败。
- (无效)下载 { NSString * imageUrl = [NSString stringWithFormat:@“%@”,txtNumber.text]; NSURLSessionConfiguration * sessionConfig = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession * session = [NSURLSession sessionWithConfiguration:sessionConfig 委托:自我 delegateQueue:无]; NSURLSessionDownloadTask * getImageTask = [会话downloadTaskWithURL:[NSURL URLWithString:imageUrl] completionHandler:^(NSURL * location, NSURLResponse *响应, NSError *错误) { UIImage * downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]]; dispatch_async(dispatch_get_main_queue(),^ { / /与图像做东西 如果(downloadedImage) { carImgView.image = downloadedImage; result = TRUE; } 其他 { 结果= FALSE; [self tryagain]; } }); }]; [getImageTask简历]; }
- (无效)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 委托:自我 delegateQueue:无]; // 1 getTryAgainTask = [tryAgainSession downloadTaskWithURL:aImgUrl completionHandler:^(NSURL * location, NSURLResponse *响应, NSError *错误) { // 2 UIImage * downloadedImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:location]]; // 3 dispatch_async(dispatch_get_main_queue(),^ { / /与图像做东西 如果(downloadedImage) { [policyImgWebView loadData:[NSData dataWithContentsOfURL:location] MIMEType:nil textEncodingName:nil baseURL:nil]; 的NSLog(@ “YES”); } 其他 { 的NSLog(@ “NO”); [self performInBackground]; } }); }]; // 4 [getTryAgainTask简历]; }
请纠正我,如果我做错了,并帮助我解决这个问题。
通过一个全局NSURLSession解决