我有AFHTTPClient的子类,主要思想是通过AFHTTPClient子类的单个实例调用所有API,所有请求都经过1分进行错误处理和显示。这是每个API调用的入口点:
-(void) makeRequestWithPath:(NSString*) path andParams:(NSDictionary*) params
success:(void (^)( id JSON, AFHTTPRequestOperation *operation)) success
failure:(void (^)( NSError *error)) failure我有很多API调用的方法,类似于:
-(void) getListMainTreeWithSuccess:(void (^)( id JSON, AFHTTPRequestOperation *operation)) success
failure:(void (^)( NSError *error)) failure
{
[self makeRequestWithPath:@"objects/selectlist" andParams:nil success:^(id JSON, AFHTTPRequestOperation *operation) {
success(JSON,operation);
} failure:^(NSError *error) {
failure(error);
}];
}这很适合我的需要。但是我面临的问题是,我需要通过我的AFHTTPClient子类在循环中发出串行请求,并在所有这些子类都完成后进行一些操作,我找到了方法
-(void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock这应该解决了我的问题,但问题是,我通过AFHTTPClient和它的方法getPath:和postPath调用所有方法:以前的方法迫使我重写所有东西,使我的子类完全无用,因为我需要在那里添加NSArray of AFHTTPRequestoperation,这是不可能从我的子类和方法中构建或提取的。以前我试着用__block来同步信号量和其他东西的请求,但是我没有得到我需要的东西,请帮帮我!
更新:看起来甚至不可能使用enqueueBatchOfHTTPRequestOperations方法(即使重写了我的所有代码),因为这个方法需要一系列http请求操作,但是不可能用它们来构造POST请求。
发布于 2014-01-10 13:22:18
我通过增加/减少挂起下载系统来解决这个问题,并将HUD绑定到这个系统上。
[networkStatus beginNetworkActivity];
[client someRESTActionWithCompletion:^(id object, NSError *error) {
[networkStatus endNetworkActivity];
if (error) {
// Handle the error ...
}
if (![networkStatus hasNetworkActivity]) {
// All downloads have finished
}
}];我将网络状态对象与AFHTTPClient子类分开,但如果您想要的话,它可以内置到客户机中。
网络状态保持内部计数器。-beginNetworkActivity增加计数器,如果计数器为0,则显示HUD。-endNetworkActivity会减少计数器,如果计数器变为0,则会取消HUD。如果计数器大于0时,-hasNetworkActivity返回YES。
其他注释:我将成功和失败的回调组合到一个single completion callback中。我将网络状态逻辑与客户端分开,因为有时我将使用单例网络状态对象,有时我将使用创建的实例,有时甚至根本不会使用。这都取决于对更高层次逻辑的需求。
发布于 2014-01-10 16:08:07
同样,正如@MikePollard所说,使用
[AFHHTPClient HTTPRequestOperationWithRequest:success:failure:]对于此方法,使用(或使用另一种方法)创建NSURLRequest,选择适合您的方法。这里还可以指定使用POST、GET或任何其他方法的方法。
[AFHTTPClient requestWithMethod:
path:
parameters:]之后,将所有操作保存到NSArray中,并使用以下方法对它们进行调度:
[AFHTTPClient enqueueBatchOfHTTPRequestOperationsWithRequests:
progressBlock:
completionBlock:]代码示例:
NSMutableArray *ops = [NSMutableArray new];
NSMutableURLRequest *request1 = [[AFHTTPClient sharedClient] requestWithMethod:@"GET"
path:@"MyEndpoint"
parameters:@{@"key1": @"value"}];
AFHTTPRequestOperation *op1 = [[AFHTTPClient sharedClient] HTTPRequestOperationWithRequest:request1
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success!");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure!");
}];
[ops addObject:op1];
NSMutableURLRequest *request2 = [[AFHTTPClient sharedClient] requestWithMethod:@"POST"
path:@"MyAnotherEndpoint"
parameters:@{@"key2": @(104)}];
AFHTTPRequestOperation *op2 = [[AFHTTPClient sharedClient] HTTPRequestOperationWithRequest:request2
success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"Success!");
}
failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Failure!");
}];
[ops addObject:op2];
[[AFHTTPClient sharedClient] enqueueBatchOfHTTPRequestOperationsWithRequests:ops
progressBlock:^(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"numberOfFinishedOperations: %d totalNumberOfOperations %d",
numberOfFinishedOperations,
totalNumberOfOperations);
}
completionBlock:^(NSArray *operations) {
NSLog(@"All operation compelted!");
}];https://stackoverflow.com/questions/21043044
复制相似问题