2012-04-07 61 views
0

我試圖使用AFNetworking類下載多個文件。我是一個完全新手,我試圖找出AFHTTPClient的工作,我已經用操作初始化了一個AFHTTPClient類的對象,但我無法弄清楚如何開始處理操作隊列。我也嘗試過查看文檔,但找不到任何東西。AFHTTPClient多文件下載

這裏是我使用的代碼:

-(void)downloadFiles:(NSArray *)files { 
    if ([files count] > 0) { 
     NSMutableArray *operationArray = [[NSMutableArray alloc] init]; 
     for (int i = 0; i < [files count]; i++) { 
      ResourceFile *resourceFile = (ResourceFile*)[files objectAtIndex:i]; 

       NSString *urlString = [NSString stringWithFormat:@"%@views/ph_session?args=0", BASE_URL]; 
       NSURL *fileDownloadUrl = [[NSURL alloc] initWithString:urlString]; 
       NSMutableURLRequest *fileDownloadRequest = [[NSMutableURLRequest alloc] initWithURL:fileDownloadUrl]; 
       [fileDownloadRequest setValue:[SessionManager getSessionCookieForLogin] forHTTPHeaderField:@"Cookie"]; 
       void (^successBlock)(NSURLRequest *, NSHTTPURLResponse *, id) = ^(NSURLRequest *request, 
                        NSHTTPURLResponse *response, 
                        id JSON) { 
        NSLog(@"Success callback for file %@", resourceFile.filename); 
       }; 
       void (^failureBlock)(NSURLRequest*, NSHTTPURLResponse*, NSError*, id) = ^(NSURLRequest *request, 
                          NSHTTPURLResponse *response, 
                          NSError *err_, 
                          id JSON) { 
        NSLog(@"Failure callback for file %@", resourceFile.filename); 
       }; 
       AFJSONRequestOperation *operation = [AFJSONRequestOperation 
                JSONRequestOperationWithRequest:fileDownloadRequest 
                success:successBlock 
                failure:failureBlock]; 
       [operationArray addObject:operation]; 
     } 
     if ([operationArray count] > 0) { 
      AFHTTPClient *requestHandler = [[AFHTTPClient alloc] init]; 
      void (^progressBlock)(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) = ^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) { 
       NSLog(@"%d proccesses completed out of %d", numberOfCompletedOperations, totalNumberOfOperations); 
      }; 
      void (^completionBlock)(NSArray *operations) = ^(NSArray *operations) { 
       NSLog(@"All operations completed"); 
      }; 

      [requestHandler enqueueBatchOfHTTPRequestOperations:operationArray 
                progressBlock:progressBlock 
               completionBlock:completionBlock]; 
     } 
    } else { 
     NSLog(@"No file to download"); 
    } 
} 

這裏是一流的resourcefile其陣列被傳遞給這個方法:

#import <Foundation/Foundation.h> 

@interface ResourceFile : NSObject { 
    NSInteger fid; 
    NSString *filemime; 
    NSString *filename; 
    NSString *filesize; 
    NSString *uri; 
    NSString *timestamp; 
} 

@property NSInteger fid; 
@property (retain, nonatomic) NSString *filemime; 
@property (retain, nonatomic) NSString *filename; 
@property (retain, nonatomic) NSString *filesize; 
@property (retain, nonatomic) NSString *uri; 
@property (retain, nonatomic) NSString *timestamp; 

-(id)initWithData:(id)data; 

@end 

可有人請告訴我,我什麼做錯了?

回答

0

我發現這個問題,我試圖用AFJSONRequestOperation AFHTTPClient enqueueBatchOfHTTPRequestOperations,在那裏我應該排隊AFHTTPOperations入隊....