管理enqueueBatchOfHTTPRequestOperationsWithRequests AFHTTPClient子类 [英] Manage enqueueBatchOfHTTPRequestOperationsWithRequests AFHTTPClient subclass

查看:88
本文介绍了管理enqueueBatchOfHTTPRequestOperationsWithRequests AFHTTPClient子类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用AFNetworking和此AFHTTPClient子类从Web服务实现多个Json请求,以创建表View。我将在MainViewController中创建tableView。

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;
    }

首先,我尝试从MainViewController调用enqueueBatchOfHTTPRequestOperationsWithRequest方法: / p>

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

我从NSLog获得的输出是:

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>>

(我有三个NSMutableRequest,但这里只显示一个)。

(I have three NSMutableRequest, but I only show one here).

我的第一个问题是,如何从NSArray操作中获取数据? NSArray中是否没有有关JSON响应的信息?如果有的话,我怎么能把它读成字典?

我的第二个问题是关于在AFHTTClient子类中实现此方法并调用它的方法从ViewController中使用委托,然后直接在ViewController中接收数据,以便我可以管理这些数据并在tableView中进行设置。

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

谢谢。

推荐答案

依次回答您的问题:


我的第一个问题是,如何从NSArray操作中获取数据?

My first question is, How can I get data from the operations NSArray?

您可以从集合中每个操作的 responseJSON 属性中获取它:

You can get this from the responseJSON property of each operation in the collection:

for (AFJSONRequestOperation *operation in operations) {
  id JSON = operation.responseJSON;
}




我的第二个问题是关于在我的AFHTTClient子类,并使用委托从ViewController调用它,并直接在ViewController中接收数据,以便我可以管理此数据并在tableView中进行设置。

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.

这篇关于管理enqueueBatchOfHTTPRequestOperationsWithRequests AFHTTPClient子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆