我想将 AFNetworking 与批处理操作一起使用。我想下载 3 个 json 文件。
如何使用 AFHTTPRequestOperation 添加基本身份验证?
NSMutableArray *mutableOperations = [NSMutableArray array];
for (NSString *fileURL in filesToDownload) {
NSURLRequest *request = [NSURLRequest
requestWithURL:[NSURL URLWithString:fileURL]];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc]
initWithRequest:request];
[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation
, id responseObject) {
NSLog(@"success: %@", operation.responseString);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"error: %@", operation.responseString);
}];
[mutableOperations addObject:operation];
}
NSArray *operations = [AFURLConnectionOperation batchOfRequestOperations:mutableOperations
progressBlock:^(NSUInteger numberOfFinishedOperations
, NSUInteger totalNumberOfOperations) {
NSLog(@"%d of %d complete", numberOfFinishedOperations, totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
NSLog(@"All operations in batch complete");
}];
[[NSOperationQueue mainQueue] addOperations:operations waitUntilFinished:NO];
非常感谢