Jordan
Jordan

Reputation: 1457

iOS Refreshing JSON Table

I'm having problems refreshing my JSON feed and updating the related table and sub views. I'm using this to call the JSON when the app first starts:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
navigationController.delegate = self;

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:THE_URL]];
NSURLConnection* topAppsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if (topAppsConnection)
    self.topAppsData = [NSMutableData data];

[self.window addSubview:loadingView];
[self.window makeKeyAndVisible];

return YES;

}

and

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSString* topAppsString = [[NSString alloc] initWithData:topAppsData encoding:NSUTF8StringEncoding];
    NSLog(@"topAppsString: %@",topAppsString);
self.topAppsData = nil;

@try {
    RootViewController* rootViewController = (RootViewController*)[navigationController.viewControllers objectAtIndex:0];
    rootViewController.topApps = [topAppsString JSONValue];
    [self.window addSubview:navigationController.view];
}
@catch (NSException * e) { }

}

And I'm using the following to do a refresh on the navigation controller:

- (void)reloadData {

NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:THE_URL]];
NSURLConnection* topAppsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[self.tableView reloadData];

if (self.refreshControl) {

    [self.refreshControl endRefreshing];
}

}

Any thoughts on what I'm missing to do the refresh? Thanks!

Upvotes: 0

Views: 39

Answers (1)

Robin
Robin

Reputation: 87

Where is your response? After NSURLRequest add this code:

NSData *response = [NSURLConnection sendSynchronousRequest:request
                                         returningResponse:nil error:nil];

NSArray *responseArray=[NSJSONSerialization JSONObjectWithData:response options:0 error:nil];
NSLog(@"%@",responseArray);

And use this array for populate table.

Upvotes: 1

Related Questions