Click here to Skip to main content
15,886,069 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
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.




Objective-C
- (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:

Objective-C
- (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];
    }
    
}
Posted
Updated 10-Apr-17 23:03pm
Comments
Mehdi Gholam 10-Nov-16 2:12am    
Your problem may not be the "getting" part but the viewing part, profile you code to see which part is slow first.

1 solution

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 .
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900