I’m trying to download multiple files using the AFNetworking classes. I’m a complete newbie to this, and I’m trying to figure out the working of AFHTTPClient, I’ve initialized an object of AFHTTPClient class with operations, but I just can’t figure out how to start processing the operations in the queue. I’ve tried looking at the documentation also, but couldn’t find anything.
Here is the code I’m using:
-(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");
}
}
Here is the ResourceFile class whose array is being passed to this method:
#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
Can anybody please tell me what I’m doing wrong?
I found the problem, I was trying to enqueue AFJSONRequestOperation using AFHTTPClient enqueueBatchOfHTTPRequestOperations, where I’m supposed to enqueue AFHTTPOperations….