如何以高效,快捷的方式从服务器获取json数据。 [英] How to GET json data from server in an efficient and faster way.?

查看:152
本文介绍了如何以高效,快捷的方式从服务器获取json数据。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hey folks, I've the following code to get data from server but it takes too long to load and show in tableView. I need it to be done in faster way.
I have my NSURLRequest in viewDidLoad.













- (void)viewDidLoad {
    
    NSNumber *getID=connectorClass.IdBeingPassed;
    NSString *getname=connectorClass.movieNameBeingPassed;
    
  
    
    NSNumber *getVote=connectorClass.voteBeingPassed;
    NSString *myNumberInString = [getVote stringValue];
    NSString *movieIDinString= [getID stringValue];
       _labelText.text=getname;
    //------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=c4bd81709e87b1208609433c49",movieIDinString];
    NSURL *url=[NSURL URLWithString:castString];

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





我尝试过:





What I have tried:

- (void)viewDidLoad {
    
    NSNumber *getID=connectorClass.IdBeingPassed;
    NSString *getname=connectorClass.movieNameBeingPassed;
    
  
    
    NSNumber *getVote=connectorClass.voteBeingPassed;
    NSString *myNumberInString = [getVote stringValue];
    NSString *movieIDinString= [getID stringValue];
       _labelText.text=getname;
    //------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=c4bd81709e87b1208609433c49",movieIDinString];
    NSURL *url=[NSURL URLWithString:castString];

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

推荐答案

我检查了你的代码。主要问题是您正在主线程中进行服务调用,这会阻止用户界面,因为只要未完成服务调用。用户界面没有出现。执行服务调用的最佳做法是在单独的线程中。您应该尝试多线程编程。对于服务调用/ api调用,他们有很多框架,可以在框架中处理所有内容。最推荐的框架是针对Objective C的AFNetworking和针对Swift的Almofire
I checked your code . The main problem is you are doing the service call in the main thread which blocking the User Interface because as long as the service call is not done . UI is not appearing. The best practice of doing the service call is in separate thread . Multi Thread Programming you should try . For service call / api call their are lots of framework which handle everything with in the framework . Most recommended framework is AFNetworking for Objective C and Almofire for Swift .


这篇关于如何以高效,快捷的方式从服务器获取json数据。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆