【发布时间】:2012-06-15 17:27:05
【问题描述】:
我正在使用 AFNetworking 库从服务器提取 JSON 提要以填充 UIPickerView,但我在理解异步处理方式时遇到了一些麻烦。 @property classChoices 是用于填充UIPickerView 的NSArray,因此网络调用只执行一次。但是,由于在返回实例变量时块还没有完成,getter 返回 nil,它最终导致我的程序稍后崩溃。任何解决此问题的帮助将不胜感激。如果您需要任何其他信息,请告诉我。
PickerViewController.m classChoices Getter
- (NSArray *)classChoices {
if (!_classChoices) {
// self.brain here refers to code for the SignUpPickerBrain below
[self.brain classChoicesForSignUpWithBlock:^(NSArray *classChoices) {
_classChoices = classChoices;
}];
}
return _classChoices;
}
SignUpPickerBrain.m
- (NSArray *)classChoicesForSignUpWithBlock:(void (^)(NSArray *classChoices))block {
[[UloopAPIClient sharedClient] getPath:@"mobClass.php" parameters:nil success:^(AFHTTPRequestOperation *operation, id responseJSON) {
NSLog(responseJSON);
if (block) {
block(responseJSON);
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
if (block) {
block(nil);
}
}];
}
【问题讨论】:
标签: objective-c ios asynchronous objective-c-blocks afnetworking