我有一个UITableViewController,称为TableViewControllerA,它是另一个对象APICallerB的委托,它是我为与API通信而创建的。通过一个NSURLSessionDataTask,APICallerB设置它的一个属性,然后将其设置为TableViewControllerA的属性之一。
下面是tableViewControllerA的viewDidLoad方法:
- (void)viewDidLoad {
[super viewDidLoad];
// init instance of APICallerB
APICallerB *acb = [[APICallerB alloc] init];
// Set TableViewControllerA as delegate
tvcA.delegate = self;
[acb makeAPICallWithArgument:self.argument];
self.property1 = acb.property2;
}我的问题是:等待[acb makeAPICallWithARgument:self.argument]完成的最佳方法是什么,这样self.property1 = acb.property2才能工作?我假设可以使用GCD (‘调度_同步’?)但是,作为iOS/Objective的新手,我不知道该在哪里使用它。还是将其中一个或两个项目移到别处比较好?
下面是来自APICallerB的方法:
- (void)makeAPICallWithArgument:(NSString *)arg
{
NSString *requestString = [NSString stringWithFormat:@"http://%@:%@@apiurl.com/json/Request?arg=%@", API_USERNAME, API_KEY, arg];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
config.HTTPAdditionalHeaders = @{@"Accept" : @"application/json"};
NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSArray *ar = [jsonObject[@"Result"] objectForKey:@"results"];
self.property2 = ar;
}];
[dataTask resume];
}发布于 2014-10-18 13:44:13
您正在调用异步方法,因此应该使用异步模式。例如,完成块实现可能如下所示:
- (void)makeAPICallWithArgument:(NSString *)arg completionHandler:(void (^)(NSArray *results, NSError *error))completionHandler
{
NSString *requestString = [NSString stringWithFormat:@"http://%@:%@@apiurl.com/json/Request?arg=%@", API_USERNAME, API_KEY, arg];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
config.HTTPAdditionalHeaders = @{@"Accept" : @"application/json"};
NSURLSession *urlSession = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
NSURL *url = [NSURL URLWithString:requestString];
NSURLRequest *req = [NSURLRequest requestWithURL:url];
NSURLSessionDataTask *dataTask = [urlSession dataTaskWithRequest:req completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (completionHandler) {
if (error) {
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(nil, error);
});
} else {
NSError *parseError;
NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
dispatch_async(dispatch_get_main_queue(), ^{
completionHandler(jsonObject[@"Result"][@"results"], parseError);
});
}
}
}];
[dataTask resume];
}你会把它叫做:
[acb makeAPICallWithArgument:self.argument completionHandler:^(NSArray *results, NSError *error){
// you'd probably first check to make sure you didn't have an error
self.property1 = results;
}];以下是几点小小的意见:
makeAPICallWithArgument正在更新一个属性,您稍后将尝试检索该属性。我将退出该属性,并将值作为参数传递回完成块。[jsonObject[@"Result"] objectForKey:@"results"]看起来不太可能是对的。您真的有JSON返回一个关键字为“结果”的字典,而在另一个字典中返回的是“结果”键。如果是的话,很好,但看起来很可疑。即使这是您的JSON格式,我也会将其简化为jsonObject[@"Result"][@"results"]。https://stackoverflow.com/questions/26439677
复制相似问题