0

我已经在 SOF 上搜索这个问题好几天了,但我仍然没有找到解决方案(说同样的问题)。

我正在制作一个在 URL 列表中同时下载 5 个图像的应用程序(每个图像都在不同的服务器上)。

我有一个ImageDownloader类子类NSOperation并实现NSURLConnectionDataDelegate.

这样我就可以在 中添加一个实例,ImageDownloader它将在. 将下载器添加到的行在这里:operationQueueViewControlleroperationQueueoperationQueue

downloader = [[ImageDownloader alloc] init];
[downloader downloadImageWithURL:[controller.URList objectForKey:[NSString stringWithFormat:@"%d",downloadIndex]] queue:queue andTag:downloadIndex + 100]; //my custom initialize
downloader.delegate = self;
[queue addOperation:downloader]; //use the addOperation method

在iOS6中一切正常,但在iOS5中(我的测试设备上的 5.0 和我的 SDK 上的 5.1)搞砸了,它只是没有通过执行这些方法接收任何响应或数据didReceiveResponsedidReceiveData这两种方法没有加入)。

超过超时后,runloop 跳转到didFailWithError方法并且程序停止。

据我了解,这意味着运行循环仍然可以运行吗?

我试图打印出来error,我得到的只是:The request timed out

当我将下载实例的数量减少到 2 时,它会运行,但不会在 >=3 下载实例的情况下运行。

还有一个信息是我的网络连接确实限制了连接数。但它在iOS6上运行良好,为什么它在iOS5上不起作用?

在下载应用程序时,我仍然可以在模拟器中加载网络。

那么这是一个什么样的问题,我该如何克服这个问题呢?

提前致谢。

*更新: *由于类很多,问题还没有被清楚地发现,我将在这里分享整个项目。你可以直接从这里下载: DownloadingImage

4

2 回答 2

0

我刚刚弄清楚我的问题出在哪里,但不是很明白为什么。

在我的ImageDownloader课堂上,我设置了一个带有donecurrentRunLoop变量的运行循环。在 main 方法中,我有一个用于强制currentRunLoop运行的 while 循环。

当我删除那些“runLoop”的东西时,该应用程序可以在 iOS6 和 iOS5 上顺利运行。

所以用这些行改变整个ImageDownloader.m然后它就可以工作了(我注释掉了一些无用的(说有害的)行):

//
//  ImageLoader.m
//  DownloadImagesTableView
//
//  Created by Viet Ta Quoc on 6/25/13.
//  Copyright (c) 2013 Viet Ta Quoc. All rights reserved.
//

#import "ImageDownloader.h"

@implementation ImageDownloader
@synthesize downloadData,delegate,queue,done,customTag;
NSRunLoop *currentRunLoop;

-(void)downloadImageWithURL:(NSString *)imageUrl queue:(NSOperationQueue*)opQueue andTag:(int)tag{
    self.customTag= tag;
    self.queue = opQueue;
//    self.done = NO;
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:imageUrl] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:30];
    NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
    [connection start];
//    currentRunLoop = [NSRunLoop currentRunLoop];
    NSLog(@"Start downloading image %d...",customTag);
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response{
    NSLog(@"Received response...");
    downloadData=[[NSMutableData alloc] initWithLength:0];
    expectedDataLength=[response expectedContentLength];
    NSLog(@"Image %d size: %lld kb",customTag,[response expectedContentLength]/1024);
}

-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{
    float receivedLenght = [data length];
    receivedDataLength=(receivedDataLength+receivedLenght);
    float progress=(float)receivedDataLength/(float)expectedDataLength;
    [delegate updateProgess:progress andIndex:[NSIndexPath indexPathForRow:customTag-100 inSection:0]];
    [self.downloadData appendData:data];
//    NSLog(@"Percentage of data received of tag %d: %f %%",self.customTag,progress*100);
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    [delegate finishedDownloadingImage:downloadData andTag:customTag];
//    done = YES;
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{
    UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"Warning" message:@"Network Connection Failed?" delegate:self cancelButtonTitle:@"Cancel" otherButtonTitles:nil, nil];
//    NSLog(@"%@",[error debugDescription]);
    NSLog(@"Connection failed! Error - %@ %@",[error localizedDescription],[[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
    [alert show];
}

-(void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge{
    NSLog(@"Got here *(*&(**&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&(*&");
}

-(void)main{
//    do{
////        NSLog(@"Running....1");
//        [currentRunLoop runUntilDate:[NSDate distantFuture]];
//        //        [currentRunLoop run];
//    } while (!done);
//    [currentRunLoop runUntilDate:[NSDate dateWithTimeIntervalSinceNow:0.01]];
}
@end

谢谢你们的支持。

==================================================== =================================

P/s:对于任何对此问题感兴趣的人,我在这里更新我的整个解决方案:DownloadImage_Final

于 2013-07-26T03:46:23.280 回答
0

正如我刚刚发现的那样,如果您使用凭据,服务器可能会每隔一段时间随机拒绝它们。因此,如果您检查以确保 previousFailureCount == 0 那么您很可能会遇到错误。

于 2013-07-26T03:01:43.283 回答