Hello I have working simple tableview json parsing codes. But i want to do reload tableview in background everytime i added dispatch_sync codes but don't working my codes under.
NSArray * jsonArray;
NSMutableArray * array1;
- (void)viewDidLoad {
[super viewDidLoad];
NSURL * url = [NSURL URLWithString:@"http://bla.com/test2.json"];
NSURLRequest * urlReq = [NSURLRequest requestWithURL:url];
NSError * error;
NSData * data = [NSURLConnection sendSynchronousRequest:urlReq returningResponse:nil error:&error];
NSDictionary * jsonDict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:&error];
NSLog(@"%@",jsonDict);
jsonArray = [jsonDict objectForKey:@"worldpopulation"];
NSLog(@"%@",jsonArray);
array1 =[[NSMutableArray alloc]init];
}
- (void)main
{
dispatch_sync(dispatch_get_main_queue(), ^{
[self.tableView reloadData];
});
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
return jsonArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *cellid=@"CustomCell";
CustomTableViewCell *cell=(CustomTableViewCell *)[tableView dequeueReusableCellWithIdentifier:cellid];;
if(cell==nil)
{
cell=[[CustomTableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellid];
}
for ( NSDictionary * dict in jsonArray) {
NSString * country = [dict objectForKey:@"country"];
[array1 addObject:country];
}
cell.nameLabel.text= [array1 objectAtIndex:indexPath.row];
return cell;
}
Main.m
#import <UIKit/UIKit.h>
#import "AppDelegate.h"
int main(int argc, char * argv[]) {
@autoreleasepool {
return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class]));
}
}
I added only needed codes . I need to fix it . Thanks for your help !