【发布时间】:2012-07-23 16:30:01
【问题描述】:
我有一个从服务器下载音乐文件 m4a 的 iPad 应用程序。我正在使用 AFHTTPRequestOperation 直接下载到我的文档目录 uisng outputStreamToFileAtPath。我不关心需要多长时间。我只需要下载文件,因为下载很可能会在几个小时后进行。当我尝试在我的 ipad 上运行它时,我收到以下错误消息。我确实得到了前 5 个和最后 5 个,但其余的超时。我的代码有问题吗?有没有办法增加超时值?或者除了AFNetworking还有什么我可以使用的吗?非常感谢任何帮助/想法。
ERROR ERROR ERROR:Error Domain=NSURLErrorDomain Code=-1001 "请求超时。"的UserInfo = 0xc6e01c0 {NSErrorFailingURLStringKey = HTTP://xxxx/Music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a,NSErrorFailingURLKey = HTTP://xxx/Music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a,NSLocalizedDescription =请求超时,NSUnderlyingError = 0xc69c950 “请求超时” } - 无法保存到路径:/var/mobile/Applications/207B2EFB-78E0-4BB2-8019-026B598ECE44/Documents/music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a
和代码:
- (void)saveFilesToDocDir
{
NSString *fileLink = @"http://xxx/Music/";
NSArray *dirPathSearch = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirPath = [dirPathSearch objectAtIndex:0];
NSString *dirPath = [docDirPath stringByAppendingPathComponent:@"music/"];
// if the sub directory does not exist, create it
NSError *error = nil;
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath:dirPath])
{
NSLog(@"%@: does not exists...will attempt to create", dirPath);
if (![fileManager createDirectoryAtPath:dirPath withIntermediateDirectories:YES attributes:nil error:&error])
NSLog(@"errormsg:%@", [error description]);
}
self.processCount = 0;
for (int i = 0; i < [self.musicFiles count]; i++)
{
NSString *filename = [self.musicFiles objectAtIndex:i];
NSString *urlPath = [NSString stringWithFormat:@"%@%@", fileLink, filename];
NSString *filePath = [dirPath stringByAppendingPathComponent:filename];
// download the song file and save them directly to docdir
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlPath]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.outputStream = [NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject)
{
self.processCount++;
NSLog(@"Song:%d Success!", processCount);
// all the files have been saved, now update the playlist
if (self.processCount == [self.musicFiles count])
{
[self updatePlaylist];
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
self.processCount++;
NSLog(@"ERROR ERROR ERROR:%@ - could not save to path:%@", error, filePath);
} ];
[operation start];
}
【问题讨论】:
-
最终使用了 NSOperationQueue - 它很慢但很有效。
-
我遇到了同样的情况,但我使用的是 AFNetworking 的 AFHTTPClient 的 operationQueue。关于为什么当我将一长串流式下载操作排入队列时,其中许多操作会因 -1001 错误而超时,还有什么进一步的想法?
-
我将 [request setTimeoutInterval:900] 添加到我的 NSMutableURLRequest。这解决了我的问题。祝你好运。
-
感谢 LittlePeculiar,您是否看到相同的行为?即超时?谢天谢地,您的建议似乎也为我解决了这个问题。不过,我仍然对根本原因感兴趣。这可能是因为启动了多个并发操作,例如一个视频,它在一段时间内消耗了大部分流媒体流量,而其他正在进行的操作超时?还是这些操作在它们开始之前就超时了?
-
还有一个奇怪的地方:其中一些超时请求甚至从未出现在 Charles Proxy 中,就好像请求在发出之前就“超时”了一样。
标签: ios ipad afnetworking