Lovely Lovely - 1 month ago 6
Objective-C Question

Code Review for filling tableview cells with response returned from server

Connection Class



-(NSDictionary *)getResponseFromSearchByRoutewithUrl:(NSString *)url :(HttpCompletionBlock)completionHandler {
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url]];
[urlRequest setHTTPMethod:@"GET"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

NSDictionary *responseDictionary;
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration];

NSURLSessionDataTask *task = [session dataTaskWithRequest: urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
//check if we encountered an error
if(error != nil){
NSLog(@"%@", [error localizedDescription]);
}else{
//get and check the HTTP status code
NSInteger HTTPStatusCode = [(NSHTTPURLResponse *)response statusCode];
if (HTTPStatusCode != 200) {
NSLog(@"HTTP status code = %ld", (long)HTTPStatusCode);
}



[task resume];
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
if(data != nil){
NSError *parseError = nil;
NSDictionary *responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
NSLog(@"The response is - %@",responseDictionary);
return completionHandler(responseDictionary,nil);

}
else {
return completionHandler(nil,error);

}
}];
}
}];
[task resume];
return responseDictionary;

}


ViewController having Table view cells Class



-(void)viewDidLoad
{
_appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
[[ConnectionManager sharedConnectionManager]getResponseFromSearchByRoutewithUrl:_urlString :^(NSDictionary *responseDictionary, NSError *error) {
if(error == nil)
{

_appDelegate.flightsArray=[responseDictionary objectForKey:@"scheduledFlights"];
NSLog(@"the flights array is%@ ", _appDelegate.flightsArray);
}
else
{
NSLog(@"Error == %@ ",error);
}

}];
}


Table View delegate class methods

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

return _appDelegate.flightsArray.count;


}


Kindly Review the code and tell why the number of rows is zero...even when the response dictionary is returning dictionary...

Wos Wos
Answer

try below code

-(void)viewDidLoad
{
     _appDelegate= (AppDelegate *)[[UIApplication sharedApplication] delegate];
     [[ConnectionManager sharedConnectionManager]getResponseFromSearchByRoutewithUrl:_urlString :^(NSDictionary *responseDictionary, NSError *error) {
     if(error == nil)
     {
          _appDelegate.flightsArray=[responseDictionary objectForKey:@"scheduledFlights"];
          NSLog(@"the flights array is%@ ", _appDelegate.flightsArray);
          [self performSelector:@selector(reloadTableview) withObject:nil afterDelay:0.2];
     }
     else
     {
          NSLog(@"Error ==  %@ ",error);
     }

     }];
}

- (void)reloadTableview{
     [self.tableview reloadData];
}