我所有的工作都很顺利,但有一点问题。我在 - (void)viewDidLoad {}中有NSURLRequest,并且需要一些时间从服务器获取数据。我希望它以异步的方式完成。如何在Objective-C中异步获取API数据?
以下是我的代码,请告诉我该怎么实现。 在此向各位致谢。 :)
- (void)viewDidLoad {
[super viewDidLoad];
[[self tableView2]setDelegate:self ];
[[self tableView2]setDataSource:self];
array=[[NSMutableArray alloc]init];
NSString *castString = [NSString stringWithFormat:@"https://api.themoviedb.org/3/movie/%@/credits?api_key=c4bd81709e87b12e6c74a08609433c49",movieIDinString];
NSURL *url=[NSURL URLWithString:castString];
NSURLRequest *request=[NSURLRequest requestWithURL:url];
connection=[NSURLConnection connectionWithRequest:request delegate:self];
if (connection)
{
webData= [[NSMutableData alloc]init];
}
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,0),^(空隙){// 后台线程 dispatch_async(dispatch_get_main_queue(),^(无效){// 运行UI更新 [_activityIndicatorImageView stopAnimating]; }); }); –
@SudheerKolasani请你解释一下,因为我是Objective-C –
的新手,它很简单,只需在后台加载url并在主线程中重新加载tableview –