如果你想同时管理多个请求,AFNetworking 让它变得非常简单,只需使用:
- (void)enqueueBatchOfHTTPRequestOperations:(NSArray *)operations
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
或者
- (void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
例子:
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];
NSURLRequest *otherRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@""]];
AFHTTPRequestOperation *operationForImages = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operationForImages setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//success of images request
self.imageDictionary = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//manage error for this request
}];
AFHTTPRequestOperation *operationForText = [[AFHTTPRequestOperation alloc] initWithRequest:otherRequest];
[operationForText setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
//success of text request
self.textDictionary = responseObject;
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//manage error for this request
}];
[[ElCuratorAPIClient sharedClient] enqueueBatchOfHTTPRequestOperations:@[operationForImages,operationForText] progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
//track progression of requests
} completionBlock:^(NSArray *operations) {
//all the request are completed
}];