1

This is my code for async fetching JSON

-(void)viewDidAppear:(BOOL)animated
{
 [_indicator startAnimating];
_indicator.hidesWhenStopped = YES;
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^{
    //Load the json on another thread

    [Constants shared].jsonData = [[NSData alloc] initWithContentsOfURL:
                                   [NSURL URLWithString:@"http://api.fessor.da.kristoffer.office/homework/index/?rp[token]=app&rp[workspace]=parent&child_id=22066&type=parent&start_date=2014-05-01&end_date=2014-05-01"]];

    //When json is loaded stop the indicator
    [_indicator performSelectorOnMainThread:@selector(stopAnimating) withObject:nil waitUntilDone:YES];

    [self declareVariables];
    [_tableView reloadData];

});
}

And it's not working for some reason. It shows the spinner, I can see that the data is fetched, it stops and hides the spinner, but the tableView is not reloaded, it's blank.

3
  • Is _tableView nil? Try calling reloadData on the main thread. Commented Jun 12, 2014 at 16:41
  • 1
    I think your problem is reloading the table on a thread that's not the main thread. Rather than using a dispatch_async and initWithContentsOfURL, to do the work, I would use NSURLConnection's sendAsynchronousRequest:queue:completionHandler: which gives you a completion block that's run on the main thread. Commented Jun 12, 2014 at 16:43
  • Doing this in viewDidAppear is probably a bad idea. Are you sure you want to refresh this view every time it becomes visible? Also, "loading" code doesn't really belong in view controllers. Commented Jun 12, 2014 at 16:47

2 Answers 2

5

You want to move all the UI stuff to the main queue. So your code should read as:

-(void)viewDidAppear:(BOOL)animated
{
    [_indicator startAnimating];
    _indicator.hidesWhenStopped = YES;
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{
    //Load the json on another thread

    [Constants shared].jsonData = [[NSData alloc] initWithContentsOfURL:
                                   [NSURL URLWithString:@"http://api.fessor.da.kristoffer.office/homework/index/?rp[token]=app&rp[workspace]=parent&child_id=22066&type=parent&start_date=2014-05-01&end_date=2014-05-01"]];
    dispatch_async(dispatch_get_main_queue(), ^{
       //When json is loaded stop the indicator
       [_indicator stopAnimating];
       [self declareVariables];
       [_tableView reloadData];
       });
    });
}

This way your JSON is loaded on the background queue and the UI is updated on the main queue.

Sign up to request clarification or add additional context in comments.

Comments

2

You should try to reload your table view on the main thread. Use GCD to dispatch on the main thread.

dispatch_async(dispatch_get_main_queue(), ^{
    [_tableView reloadData];
});

If there's no result, try to log in the cellForRowAtIndexPath method to check whether the reload is done or not. If so, you error is elsewhere.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.