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



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

错误错误错误:错误域=NSURLDomain Code=-1001"请求超时。"UserInfo=0xc6e01c0{NSErrorFailingURLStringKey=http://xxxx/Music/ece0b7c5ab71a24c6f6694986fc7a4a7.m4a,NSErrorFailing URLKey=http://xxxx/Music/ece0b7c5a b71a24c6b6694986fc 7a4a4.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];
    }
[request setTimeoutInterval:1000];

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

最新更新