在另一个请求中拥有HTTP请求,然后更新UI

时间:2013-05-11 21:11:19

标签: ios objective-c asynchronous nsurlconnection

我在另一个请求中有一个HTTP请求,因为第二个请求依赖于第一个请求的响应。这将包含在将在多个视图控制器中使用的单个方法中。

在另一个请求中异步包装请求的最佳方法是什么? 我想根据第二个请求的响应更新iOS应用的UI。同时我不希望每个视图控制器中都有“请求代码”,因为我希望它是自己的另一个类。解耦UI视图控制器代码和请求代码的最佳方法是什么?

由于

1 个答案:

答案 0 :(得分:0)

我使用AFNetworking

通常你是AFHTTPClient的子类,但这是一个简单的例子

AFHTTPClient *client = [[AFHTTPClient alloc] initWithBaseURL:baseURL];
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];
[client setDefaultHeader:@"Accept" value:@"application/json"];

[client getPath:@"api1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [client getPath:@"api2" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Yay! success!!
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // ERROR in API 2
    }];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // ERROR in API 1
}];

AFHTTPClient的示例子类,简化了设置。

@interface MyHTTPClient : AFHTTPClient
@end

@implementation MyHTTPClient
- (id)initWithBaseURL:(NSURL *)baseURL
{
    self = [super initWithBaseURL:baseURL];
    if (self) {
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }
    return self;
}
@end

现在在视图控制器中,跳过设置操作类和Accept标头。

MyHTTPClient *client = [[MyHTTPClient alloc] initWithBaseURL:baseURL];

[client getPath:@"api1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
    [client getPath:@"api2" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        // Yay! success!!
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        // ERROR in API 2
    }];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    // ERROR in API 1
}];

以下示例简化了设置并创建了自定义客户端API。

typedef void (^MySuccess)(id JSON);
typedef void (^MyFailure)(NSError *error);

@interface MyJSONClient : AFHTTPClient
- (void)getJSONSuccess:(MySuccess)success failure:(MyFailure)failure;
@end

@implementation MyJSONClient
- (void)getJSONSuccess:(MySuccess)success failure:(MyFailure)failure
{
    [self getPath:@"api1" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        [self getPath:@"api2" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
            success(responseObject);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            failure(error);
        }];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            failure(error);
    }];
}
- (id)initWithBaseURL:(NSURL *)baseURL
{
    self = [super initWithBaseURL:baseURL];
    if (self) {
        [self registerHTTPOperationClass:[AFJSONRequestOperation class]];
        [self setDefaultHeader:@"Accept" value:@"application/json"];
    }
    return self;
}
@end

现在在视图控制器中,使用自定义方法。

MyJSONClient *client = [[MyJSONClient alloc] initWithBaseURL:baseURL];

[client getJSONSuccess:^(id JSON) {
    // Yay! success!!
} failure:^(NSError *error) {
    // ERROR in API
}];