问题
I want to implement multiple Json requests from a web service with AFNetworking and this AFHTTPClient subclass to create a table View. I will create the tableView in MainViewController.
#import "AFHTTPClient.h"
@interface YlyHTTPClient : AFHTTPClient
+ ( YlyHTTPClient *)sharedHTTPClient;
- (id)initWithBaseURL:(NSURL *)url;
@end
#import "YlyHTTPClient.h"
static NSString * const urlString = @"http://localhost/example/";
@implementation YplyHTTPClient
+ (YlyHTTPClient *)sharedHTTPClient {
static YeeplyHTTPClient *_httpClient = nil;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_httpClient = [[YlyHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:urlString]];
[_httpClient setParameterEncoding:AFJSONParameterEncoding];
[_httpClient registerHTTPOperationClass:[AFJSONRequestOperation class]];
});
return _httpClient;
}
-(id)initWithBaseURL:(NSURL *)url {
self = [super initWithBaseURL:url];
if (!self) {
return nil;
}
[self registerHTTPOperationClass:[AFJSONRequestOperation class]];
[self setDefaultHeader:@"Accept" value:@"application/json"];
return self;
}
First I've tried to call enqueueBatchOfHTTPRequestOperationsWithRequest method from the MainViewController doing this:
- (void)viewDidLoad
{
NSMutableArray *mutableRequests = [NSMutableArray array];
for (NSString *URLString in [NSArray arrayWithObjects:@"users", @"projects", @"interestedUsers", nil]) {
[mutableRequests addObject:[[YlyHTTPClient sharedHTTPClient] requestWithMethod:@"GET" path:URLString parameters:nil]];
}
[[YlyHTTPClient sharedHTTPClient] enqueueBatchOfHTTPRequestOperationsWithRequests:mutableRequests progressBlock:^(NSUInteger numberOfCompletedOperations, NSUInteger totalNumberOfOperations) {
NSLog(@"%lu of %lu Completed", (unsigned long)numberOfCompletedOperations, (unsigned long)totalNumberOfOperations);
} completionBlock:^(NSArray *operations) {
NSLog(@"Completion: %@", [operations objectAtIndex:1]);
}];
[super viewDidLoad];
}
And the output that I got from the NSLog is:
Completion: <AFJSONRequestOperation: 0x75dbe60, state: isFinished, cancelled: NO request: <NSMutableURLRequest http://localhost/yeeply_service/api/example/projects>, response: <NSHTTPURLResponse: 0x72cd000>>
(I have three NSMutableRequest, but I only show one here).
My first question is, How can I get data from the operations NSArray? is there no information of the JSON response in the NSArray? If there is, how can I read it as a dictionary?
My second question is about implement this method in my AFHTTClient subclass and call it from the ViewController using delegates, and receiving data directly in the ViewController, so that I can manage this data and set it in the tableView.
-(void)enqueueBatchOfHTTPRequestOperationsWithRequests:(NSArray *)urlRequests
progressBlock:(void (^)(NSUInteger numberOfFinishedOperations, NSUInteger totalNumberOfOperations))progressBlock
completionBlock:(void (^)(NSArray *operations))completionBlock;
Thank you.
回答1:
Answering your questions in turn:
My first question is, How can I get data from the operations NSArray?
You can get this from the responseJSON
property of each operation in the collection:
for (AFJSONRequestOperation *operation in operations) {
id JSON = operation.responseJSON;
}
My second question is about implement this method in my AFHTTClient subclass and call it from the ViewController using delegates, and receiving data directly in the ViewController, so that I can manage this data and set it in the tableView.
In your subclass, create a new method with a callback block parameter with the information you need, and call that block at the end of the batch completion block.
来源:https://stackoverflow.com/questions/16569939/manage-enqueuebatchofhttprequestoperationswithrequests-afhttpclient-subclass