AFHTTPClient多文件下载

时间:2012-04-07 10:17:43

标签: objective-c ios afnetworking

我正在尝试使用AFNetworking类下载多个文件。我是一个完全新手,我正在试图弄清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

有人可以告诉我我做错了吗?

1 个答案:

答案 0 :(得分:0)

我发现了问题,我试图使用AFHTTPClient enqueueBatchOfHTTPRequestOperations将AFJSONRequestOperation排入队列,我应该将AFHTTPOperations排入队列....