将块传递给AFNetworking方法?

Seg*_*gev 5 objective-c objective-c-blocks afnetworking

-(void )getDataFromServer: (NSMutableDictionary *)dict
 {

 NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/doSomething",MainURL ]];
 [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];

 AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
 NSMutableURLRequest *request = [httpClient requestWithMethod:@"POST" path:nil parameters:dict];

 AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
 success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON)
 {
     _myArray = JSON;
     [_myTableView reloadData]; //Or do some other stuff that are related to the current `ViewController`
 }
 failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON)
 {
     NSLog(@"request: %@",request);
     NSLog(@"Failed: %@",[error localizedDescription]);
 }];

 [httpClient enqueueHTTPRequestOperation:operation];
 }
Run Code Online (Sandbox Code Playgroud)

我在我的一个应用程序中的7个不同位置使用上面的代码.确切的代码块在我的7个中重复ViewControllers.我通常做的是将我想要在NSObject类中使用的方法放在我需要的时候分配和使用它,但因为上面是异步并使用块我不能只是将JSON返回给ViewController谁调用它并且必须在ViewController我需要的每一个中复制和粘贴上述方法.

我的目标是在我的应用程序中只有一个位置,并且仍然可以从ViewControllers我的应用程序周围的不同地调用它,并获得我需要的数据.我想避免使用像NSNotificationKVO等的观察者来寻找更优雅的解决方案.经过一番阅读后,我注意到有可能绕过街区.这可能是上述的解决方案吗?一个代码示例将不胜感激.

Gab*_*lla 6

将API调用分解为类似的东西

+ (void)getDataFromServerWithParameters:(NSMutableDictionary *)params completion:(void (^)(id JSON))completion failure:(void (^)(NSError * error))failure {
     NSString * path = @"resources/123";
     NSMutableURLRequest *request = [self.httpClient requestWithMethod:@"POST" path:path parameters:params];
     AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        if (completion)
            completion(JSON);
     } failure:^(NSURLRequest *request , NSURLResponse *response , NSError *error , id JSON) {
        if (failure)
            failure(error);
     }];

     [httpClient enqueueHTTPRequestOperation:operation];
 }
Run Code Online (Sandbox Code Playgroud)

您可以将此方法放在类似的实用程序类中XYAPI,只需从控制器中调用它即可

 [XYAPI getDataFromServer:params completion:^(id JSON){
     // do something, for instance reload the table with a new data source
     _myArray = JSON;
     [_myTableView reloadData];
 } failure:^(NSError * error) {
    // do something
 }];
Run Code Online (Sandbox Code Playgroud)

此外,您不需要AFHTTPClient在每个请求中生成新的.在XYAPI类中配置和使用共享的.就像是

+ (AFHTTPClient *)httpClient {
    static AFHTTPClient * client = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        client = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:@"http://foo.com/api/v1/"]];
        [AFJSONRequestOperation addAcceptableContentTypes:[NSSet setWithObject:@"text/html"]];
    });
    return client;
}
Run Code Online (Sandbox Code Playgroud)

请注意,此实现已在上面的示例中使用.
self在类方法的上下文中是类本身,因此self.httpClient确实在运行时解析为[XYAPI httpClient].