Objective c 将块传递给AFN网络方法?
我在我的一个应用程序中的7个不同地方使用了上述代码。我的7个Objective c 将块传递给AFN网络方法?,objective-c,afnetworking,objective-c-blocks,Objective C,Afnetworking,Objective C Blocks,我在我的一个应用程序中的7个不同地方使用了上述代码。我的7个视图控制器中复制了精确的代码块。我通常要做的是将我想要使用的方法放在NSObject类中,并在需要时分配和使用它,但因为上面的方法是异步的,并且使用块,所以我不能只将JSON返回给调用它的ViewController,并且必须在我需要它的每个ViewController中复制并粘贴上面的方法 我的目标是在我的应用程序中只有一个地方有上述内容,并且仍然能够从我的应用程序周围的不同ViewControllers调用它,并获取我需要的数据。
视图控制器
中复制了精确的代码块。我通常要做的是将我想要使用的方法放在NSObject类中,并在需要时分配和使用它,但因为上面的方法是异步的,并且使用块,所以我不能只将JSON返回给调用它的ViewController
,并且必须在我需要它的每个ViewController
中复制并粘贴上面的方法
我的目标是在我的应用程序中只有一个地方有上述内容,并且仍然能够从我的应用程序周围的不同ViewControllers
调用它,并获取我需要的数据。
我希望避免使用观察器,例如NSNotification
或KVO
,并寻找更优雅的解决方案。
读了一些书之后,我注意到了绕过街区的可能性。这是解决上述问题的可能办法吗?请提供一个代码示例。将API调用分解为
-(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];
}
[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
}];
您可以将此方法放置在一个实用程序类(如XYAPI
)中,然后从控制器(如
+ (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];
}
此外,您不需要在每次请求时生成新的AFHTTPClient
。在XYAPI
类中配置并使用共享的。差不多
-(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];
}
[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
}];
请注意,上述示例中已经使用了此实现。self
在类方法的上下文中是类本身,因此self.httpClient
确实在运行时被解析为[XYAPI httpClient]
除了下面Gabriele的答案之外,您可能希望避免为每个请求旋转一个新的AFHTTPClient。我正是为此编辑我的答案的。谢谢你的留言。回答得很好!非常详细,写得很好。我会测试一下,然后再报告。谢谢。