質問

I've got subclass of AFHTTPClient The main idea is that i call all API through my singleton of AFHTTPClient subclass, and all requests goes through 1 points for error handling and HUD displaying. This is entry point for every API calls:

-(void) makeRequestWithPath:(NSString*) path andParams:(NSDictionary*) params 
                    success:(void (^)( id JSON, AFHTTPRequestOperation *operation)) success
                    failure:(void (^)( NSError *error)) failure

And i've got many methods for API calls something like that:

-(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);
}];

}

This works just fine for my needs. But i faced problem that i need to make serial request in loop through my AFHTTPClient subclass and make some action when all of them are finished , I found method

-(void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
                                      progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
                                    completionBlock:(void (^)(NSArray *operations))completionBlock

which should solve my issue, but the problem is that i call all methods through AFHTTPClient and it's methods getPath: and postPath: and previous way forces me to rewrite everything and makes my subclass completely useless, because I need to add there NSArray of AFHTTPRequestoperation, which is not possible to construct or extract from my subclass and my methods. Previously i tried to use __block 's to synchronise requests with semaphore and something else but i failed to get what i need, please help me!

UPDATE: It seems that it is not possible to even use enqueueBatchOfHTTPRequestOperations method (even with rewriting all my code) because this method needs array of http request operations, but it's not possible to construct POST request with them.

役に立ちましたか?

解決

I solved this with an increment/decrement pending download system and tied the HUD to that.

[networkStatus beginNetworkActivity];
[client someRESTActionWithCompletion:^(id object, NSError *error) {
    [networkStatus endNetworkActivity];

    if (error) {
        // Handle the error ...
    }

    if (![networkStatus hasNetworkActivity]) {
        // All downloads have finished
    }
}];

I keep the network status object separate which from the AFHTTPClient subclass, but it can be built into the client if that's what you want.

Network status keeps an internal counter. -beginNetworkActivity increments the counter, if the counter was 0, then it displays a HUD. -endNetworkActivity decrements the counter, if the counter becomes 0, then it dismisses the HUD. -hasNetworkActivity returns YES if the counter greater than 0.

Other Notes: I combine the success and failed callbacks into a single completion callback. I keep the network status logic separate from the client because sometime I'll use a singleton network status object, sometimes I'll use a created instance, sometimes I won't use one at all. It all depends on the needs to the higher level logic.

他のヒント

Again, as @MikePollard said, create AFHTTPRequestOperation using

[AFHHTPClient HTTPRequestOperationWithRequest:success:failure:]

For this method create NSURLRequest using (or use another one, pick which one is suitable for you). Here you can also specify, which method to use POST, GET or any other.

[AFHTTPClient requestWithMethod:
                           path:
                     parameters:]

After that save all operation to an NSArray, and schedule them using:

[AFHTTPClient enqueueBatchOfHTTPRequestOperationsWithRequests:
                                                progressBlock:
                                              completionBlock:]

Code example:

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!");
                                                             }];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top