如何将AFNetworking AFJSONRequestOperation包装到单独的datamanager类中

Dav*_*ave 2 ios objective-c-blocks afnetworking

我是新手在iOS中使用块,我认为这可能是我的问题的症结所在.

我只想构建一个简单的静态DataManager类,其唯一的工作是从Restful服务中获取数据.

我会从我所有的各种UIViewControllers(或collectionview/table控制器)中调用它

在我的课堂上,我有一个看起来像这样的功能

+ (NSArray *) SearchByKeyword: (NSString*) keyword {
    __block NSArray* searchResults = [[NSArray alloc] init];
    NSString *baseURL = @"http://someURL.com/api/search";
    NSString *requestURL = [baseURL stringByAppendingString:keyword];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                      path:requestURL
                                                      parameters:nil];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
        searchResults = [JSON valueForKeyPath:@""];
    } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
        NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
    }];
    [operation start];
    return searchResults;
}
Run Code Online (Sandbox Code Playgroud)

但是,这会保持返回零数据.有人可以提出正确的方法吗?

Car*_*zey 7

您正在尝试使用异步任务的结果(JSON操作)作为同步方法调用的返回值,这就是您没有数据的原因.

您可以为视图控制器提供一个API,该API采用完成块和故障块,类似于AF网络.然后,视图控制器可以在将结果传递到块中时执行结果所需的操作.

修改您的问题中的代码:

typedef void (^SearchCompletionBlock)(NSArray *results);
typedef void (^SearchFailureBlock)(NSError *error);

+ (void)searchByKeyword:(NSString*)keyword completionBlock:(SearchCompletionBlock)completionBlock failureBlock:(SearchFailureBlock)failureBlock;
{
    NSString *baseURL = @"http://someURL.com/api/search";
    NSString *requestURL = [baseURL stringByAppendingString:keyword];
    AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:[NSURL URLWithString:baseURL]];
    NSMutableURLRequest *request = [httpClient requestWithMethod:@"GET"
                                                            path:requestURL
                                                      parameters:nil];
    AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                                        success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
                                                                                            if (completionBlock) {
                                                                                                completionBlockc([JSON valueForKeyPath:@""]);
                                                                                            }
                                                                                        } failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                                            NSLog(@"Request Failed with Error: %@, %@", error, error.userInfo);
                                                                                            if (failureBlock) {
                                                                                                failureBlock(error);
                                                                                            }
                                                                                        }];
    [operation start];
}
Run Code Online (Sandbox Code Playgroud)

然后,客户端可以传递存储结果的完成块并重新加载其视图.就像是:

^ (NSArray *results) {
    self.results = results;
    [self.tableView reloadData];
}
Run Code Online (Sandbox Code Playgroud)