2016-11-10 28 views
0

我想获取数据并在tableView中显示。因为我必须在viewDidLoad中发送请求。但是加载我的数据花了很多时间,所以我需要以异步的方式完成。 任何建议或建议。?如何使用异步方式从API获取数据?

- (void)viewDidLoad { 

    //------castApi----// 
    [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=c4bd81709e87b12e609433c49",movieIDinString]; 
    NSURL *url=[NSURL URLWithString:castString]; 

    NSURLRequest *request=[NSURLRequest requestWithURL:url]; 
    connection=[NSURLConnection connectionWithRequest:request delegate:self]; 
    if (connection){ 
     webData= [[NSMutableData alloc]init]; 
    } 
} 

回答

0

试试这个

NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://api.themoviedb.org/3/movie/%@/credits?api_key=c4bd81709e87b12e609433c49",movieIDinString]]; 

NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; 
NSOperationQueue *queue = [[NSOperationQueue alloc] init]; 

[NSURLConnection sendAsynchronousRequest:urlRequest queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) 
{ 

}]; 
+0

什么应该保持在sendAsynchronousRequest?对不起,愚蠢的问题,因为我是新手:) –

+0

您的请求网址老板:) @ SheikhMuhammadAmaan-Ullah –

+0

什么是与URL请求的问题? – KKRocks

相关问题