2014-10-08 41 views
0

因此,我正處於一種情況,在進入主屏幕之前,我已經在iOS應用程序中下載了多個視頻文件,並且正在使用AFNetworking 2。如果我必須下載一個文件一切都很好,但是當我不得不下載多個文件時出現此錯誤失敗,它並不總是相同的文件,它似乎是隨機的..:AFNetworking下載循環失去驗證

錯誤:錯誤域= com.alamofire.error.serialization.response代碼= -1011「請求失敗:未授權(401)」UserInfo = 0x7ff943f4fcb0 {NSLocalizedDescription =請求失敗:未授權(401),com.alamofire.serialization.response.error.response =

說我使用此代碼:

MainView *mainViewController = [[MainView alloc] init]; 

NSURLCredential *credential = [NSURLCredential credentialWithUser:AUTH_USERNAME2 password:AUTH_PASSWORD2 persistence:NSURLCredentialPersistenceForSession]; 

NSFileManager *fileManager = [NSFileManager defaultManager]; 
[fileManager changeCurrentDirectoryPath:self.topVideoPath]; 


__block int countVideos = 0; 
__block int totalvideos = [videoItems count]; 
for (int i = 0; i < [videoItems count]; i++){ 

    NSLog(@"dict array : %@" , self.dictArray); 
    NSMutableDictionary *itemDict = [self.dictArray objectAtIndex:i]; 
    NSURL *url = [NSURL URLWithString:[itemDict objectForKey:@"video"]]; 
    NSString *videoString = [itemDict objectForKey:@"video"]; 
    NSString *specificPath = [[fileManager currentDirectoryPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.dat", [itemDict objectForKey:@"id"]]]; 
    TopItem *item = [videoItems objectAtIndex:i]; 

      if (![videoString isEqualToString:@""]) { 

       [fileManager changeCurrentDirectoryPath:self.topVideoPath]; 

       NSString *moviePath = [[fileManager currentDirectoryPath] stringByAppendingPathComponent:[NSString stringWithFormat:@"Movie%@.mp4", [itemDict objectForKey:@"id"]]]; 
       NSString *correctMoviePath = [NSString stringWithFormat:@"KyoskSkeleton/TopVideo/Movie%@.mp4", [itemDict objectForKey:@"id"]]; 

       AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:url]]; 
       [operation setCredential:credential]; 
       operation.outputStream = [NSOutputStream outputStreamToFileAtPath:moviePath append:NO]; 
       [operation setDownloadProgressBlock:^(NSUInteger bytesRead, long long totalBytesRead, long long totalBytesExpectedToRead) { 

        float progress = ((float)totalBytesRead)/totalBytesExpectedToRead; 
        NSLog(@"The downloading progress : %f", progress); 


       }]; 
       [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

        countVideos = countVideos + 1; 

        NSLog(@"specific path : %@", specificPath); 
        [NSKeyedArchiver archiveRootObject:item toFile:specificPath]; 

        if (countVideos == totalvideos){ 
        mainViewController.topScrollViewContent = self.topScrollViewContent; 
         if ([self.responseDelegate respondsToSelector:@selector(pushController:)]) { 
          [self.responseDelegate performSelector:@selector(pushController:) withObject:mainViewController]; 
         } 
        } 
       } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
        NSLog(@"Not Downloaded"); 
        NSLog(@"Error : %@", error); 

        countVideos = countVideos + 1; 
       }]; 

       [operation start]; 
} 
} 

有誰知道是怎麼回事了?

在此先感謝!

回答

0

最後,我用maxConcurrentOperationCount = 3的NSOperationque修復了它,因此只有3個視頻同時下載。服務器可能會遇到很多連接問題。