Ios 如何在加载tableview之前执行API方法?
要求是在应用程序启动后立即调用edmunds API并在表中显示供应商名称 1) -getAPIData() 检索self.dealerName数组中经销商和门店的名称Ios 如何在加载tableview之前执行API方法?,ios,objective-c,Ios,Objective C,要求是在应用程序启动后立即调用edmunds API并在表中显示供应商名称 1) -getAPIData() 检索self.dealerName数组中经销商和门店的名称 -(void)getAPIData{ NSURLSession *session = [NSURLSession sharedSession]; self.task = [session dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat
-(void)getAPIData{
NSURLSession *session = [NSURLSession sharedSession];
self.task = [session dataTaskWithURL:[NSURL URLWithString:[NSString stringWithFormat:
@"https://api.edmunds.com/api/dealer/v2/dealers?zipcode=%@&radius=%@&fmt=json&api_key=ycwedw68qast829zx7sn9jnq",
@"01609",@"10"]]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
if (data.length > 0 && error == nil)
{
NSDictionary *jsonData = [NSJSONSerialization JSONObjectWithData:data
options:kNilOptions
error:NULL];
self.dealersDictionary = jsonData[@"dealers"];
for (id item in self.dealersDictionary){
if (item[@"name"] != nil){
self.dealerName = item[@"name"];
NSLog(@"%@",self.dealerName);
}else{
NSLog(@"could'nt find the names");
}
}
}
}
];
}
2) -viewDidLoad()
此方法调用getAPIData(上述方法)
3) -(NSInteger)表视图:(UITableView*)表视图行数节:(NSInteger)节
返回经销商计数
-(NSInteger)tableView:(UITableView *)tableview numberOfRowsInSection:(NSInteger)section
{
[self.task resume];
return self.dealerName.count;
}
调用numberOfRowsInSection()后,将执行getAPIData()方法。因此,表被呈现为空
在将表加载到屏幕上之前,如何调用getAPIData()。我认为这里的问题是
NSURLSession dataTaskWithURL
是异步的
我认为您可以继续使用异步方法并调用[self.tableView reloadData]代码>调用成功返回后(例如,在完成处理程序中)。您可以在等待加载完成时显示活动指示器
或者,您可以发出一个阻塞、同步请求,如下所示:
// define the URL
NSURL url = [NSURL URLWithString: @"https://api.edmunds.com/api/dealer/v2/dealers?zipcode=%@&radius=%@&fmt=json&api_key=ycwedw68qast829zx7sn9jnq"];
// attempt the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];
[request setHTTPMethod: @"GET"];
NSError *reqError;
NSURLResponse *urlResponse;
NSData *returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&reqError];
// parse `returnedData` here
现在,您将拥有NSData
对象,您可以在这里解析它,就像在completionHandler
中一样。如果您相信您的请求-响应将快速返回,则此选项是可行的
最后一个选项:使用NSURLConnection
执行异步请求,并使用其-(void)连接:(NSURLConnection*)连接didReceiveData:(NSData*)数据
委托方法更新UIProgressView
,以获得更精确的活动指示器。如果您的请求-响应时间足够长,让用户怀疑是否发生了任何事情(例如,超过几秒钟),我鼓励您研究此方法。简短回答:您没有。显示一个空的表视图,然后在完成块中,调用tableView的reloadData方法,然后加载其内容。您还可以将响应存储在缓存中,以便下次启动应用程序时,在等待新数据时立即显示数据。这将是相当容易做到的,因为你已经硬编码的邮政编码无论如何。谢谢你的回答帮助了我。
// define the URL
NSURL url = [NSURL URLWithString: @"https://api.edmunds.com/api/dealer/v2/dealers?zipcode=%@&radius=%@&fmt=json&api_key=ycwedw68qast829zx7sn9jnq"];
// attempt the request
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10];
[request setHTTPMethod: @"GET"];
NSError *reqError;
NSURLResponse *urlResponse;
NSData *returnedData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&reqError];
// parse `returnedData` here