AFNetworking无法在ios iPad中下载多个m4a文件

AFNetworking无法在ios iPad中下载多个m4a文件,ios,ipad,afnetworking,Ios,Ipad,Afnetworking,我有一个iPad应用程序,可以从服务器下载音乐文件m4a。我正在使用AFHTTPRequestOperation直接下载到我的文档目录,使用outputStreamToFileAtPath。我不在乎需要多长时间。我只需要下载的文件,因为下载很可能会在几个小时后进行。当我尝试在我的ipad上运行它时,我收到以下错误消息。我确实得到了前5个和最后5个,但其余的都超时了。我的代码有问题吗?有没有办法增加超时值?或者除了网络之外,还有什么我可以使用的吗?非常感谢您的任何帮助/想法 错误:错误域=NSUR

我有一个iPad应用程序,可以从服务器下载音乐文件m4a。我正在使用AFHTTPRequestOperation直接下载到我的文档目录,使用outputStreamToFileAtPath。我不在乎需要多长时间。我只需要下载的文件,因为下载很可能会在几个小时后进行。当我尝试在我的ipad上运行它时,我收到以下错误消息。我确实得到了前5个和最后5个,但其余的都超时了。我的代码有问题吗?有没有办法增加超时值?或者除了网络之外,还有什么我可以使用的吗?非常感谢您的任何帮助/想法

错误:错误域=NSURErrorDomain代码=-1001“请求超时。”用户信息=0xc6e01c0{NSERRORFAILLINGURLSTRINGKEY=http://xxxx/Music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a,n错误失败键=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];
    }
-(void)saveFilesToDocDir
{
NSString*文件链接=@“http://xxx/Music/";
NSArray*dirPathSearch=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,是);
NSString*docDirPath=[dirPathSearch objectAtIndex:0];
NSString*dirPath=[docDirPath stringByAppendingPathComponent:@“music/”];
//如果子目录不存在,请创建它
n错误*错误=nil;
NSFileManager*fileManager=[NSFileManager defaultManager];
如果(![fileManager fileExistsAtPath:dirPath])
{
NSLog(@“%@:不存在…将尝试创建“,”dirPath);
if(![fileManager createDirectoryAtPath:dirPath with IntermediateDirectory:YES属性:无错误:&错误])
NSLog(@“errormsg:%@,[错误描述]);
}
self.processCount=0;
for(int i=0;i<[self.music文件计数];i++)
{
NSString*filename=[self.musicFiles objectAtIndex:i];
NSString*urlPath=[NSString stringWithFormat:@“%@%@”,文件链接,文件名];
NSString*filePath=[dirPath stringByAppendingPathComponent:filename];
//下载歌曲文件并直接保存到docdir
NSURLRequest*request=[NSURLRequest requestWithURL:[NSURL URLWithString:urlPath]];
AFHTTPRequestOperation*操作=[[AFHTTPRequestOperation alloc]initWithRequest:request];
operation.outputStream=[NSOutputStream outputStreamToFileAtPath:filePath append:NO];
[操作setCompletionBlockWithSuccess:^(AFHTTPRequestOperation*操作,id响应对象)
{
self.processCount++;
NSLog(@“歌曲:%d成功!”,processCount);
//所有文件都已保存,现在更新播放列表
if(self.processCount==[self.musicFiles count])
{
[自更新播放列表];
}
}失败:^(AFHTTPRequestOperation*操作,NSError*错误)
{
self.processCount++;
NSLog(@“错误:%@-无法保存到路径:%@”,错误,文件路径);
} ];
[操作启动];
}

为间隔设置适当的值(可以使用此值),在创建请求后添加。

最终使用了NSOperationQueue-速度很慢,但很有效。我遇到了同样的情况,但使用的是AFNetworking的AFHTTPClient的operationQueue。我补充道,当我将一长串流媒体下载操作排入队列时,有没有进一步的想法,为什么其中许多操作会因-1001错误而超时[请求setTimeoutInterval:900]对我的NSMutableURLRequest。这解决了我的问题。祝你好运。感谢LittlePeculiar,你看到了相同的行为吗?即超时?谢天谢地,你的建议似乎也解决了我的问题。不过我仍然对根本原因感兴趣。这可能是多个并发操作启动的结果,其中一个是视频吗例如,它在一段时间内消耗了大部分流媒体流量,而另一个正在进行的操作超时了?或者这些操作在启动之前就超时了?还有一个奇怪的现象:有些超时请求甚至从未出现在Charles Proxy中,就好像请求在发出之前“超时”。
[request setTimeoutInterval:1000];