gpt4 book ai didi

iphone - 创建一个类以从 Web 服务上的异步调用返回数据

转载 作者:行者123 更新时间:2023-11-28 20:21:18 24 4
gpt4 key购买 nike

我正在编写一个使用 Web 服务获取一些 JSON 数据的应用程序,我将需要跨不同的 View Controller 从 Web 服务获取不同的数据。所以我想创建一个类来处理这个问题,目的是在将来发布它。

在我的类(class)中,我想利用 AFJSONRequestOperationAFNetworking 框架从 Web 服务获取 JSON 数据,但这会异步返回数据,所以不是不仅仅是在类方法上返回数据那么简单。

如何让我的类处理这些数据并将其传递回调用类?在传回数据时,我是否必须像往常一样使用委托(delegate),还是有其他方法?

+(NSDictionary*)fetchDataFromWebService:(NSString *)query{

NSURL *url = [NSURL URLWithString:query];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Success");


} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Fail");
}];

[operation start];

return ??? // I can't return anything here because AFJSONRequestOperation is completed Async
}

所以我应该这样做,并使用委托(delegate)

+(void)fetchDataFromWebService:(NSString *)query{

NSURL *url = [NSURL URLWithString:query];

NSURLRequest *request = [NSURLRequest requestWithURL:url];

AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
NSLog(@"Success");
[self.delegate didFinishFetchingJSON:(NSDictionary*)json];

} failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
NSLog(@"Fail");
[self.delegate didFinishFetchingJSON:(NSDictionary*)json withError:(NSError*)error];
}];

[operation start];
}

任何有关使用异步调用创建此类类的最佳方法和最佳实践的帮助都将非常有帮助。

非常感谢

最佳答案

当您像这样执行异步调用时,绝对不要期望使用传统的返回设置。您的委托(delegate)想法肯定会奏效,您也可以将数据作为 @property 或其他方式传递。但我更喜欢的方式是这样的:

 - (void)postText:(NSString *)text
forUserName:(NSString *)username
ADNDictionary:(NSDictionary *)dictionary
withBlock:(void(^)(NSDictionary *response, NSError *error))block;

我用一个 block 作为参数来声明方法。实现看起来像这样:

- (void)postText:(NSString *)text
forUserName:(NSString *)username
ADNDictionary:(NSDictionary *)dictionary
withBlock:(void(^)(NSDictionary *response, NSError *error))block
{
// Custom logic

[[KSADNAPIClient sharedAPI] postPath:@"stream/0/posts"
parameters:params
success:^(AFHTTPRequestOperation *operation, id responseObject)
{
if (block) {
block(responseObject, nil);
}
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
if (block) {
block([NSDictionary dictionary], error);
}
}];
}

如您所见,一旦我从 Web 服务获得响应,我就会将对象传回调用它的 block 中。我用以下方法调用此方法:

[[KSADNAPIClient sharedAPI] postText:postText
forUserName:username
ADNDictionary:parameters
withBlock:^(NSDictionary *response, NSError *error)
{
if (error) {
// Handle error
} else {
// Do other stuff
}
}];

所以一旦你调用它,这个 block 就不会做任何事情,直到它得到服务的响应。然后在这个 block 中,如果你愿意,你可以调用类似的东西:

[self loadInfo:response];

关于iphone - 创建一个类以从 Web 服务上的异步调用返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15813423/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com