1
   NSURL *url=[NSURL URLWithString:string];
    dispatch_queue_t backgroundQueue = dispatch_queue_create("com.example.workQueue", DISPATCH_QUEUE_SERIAL);    
    dispatch_async(backgroundQueue, ^{
        NSData *data=[NSData dataWithContentsOfURL:url];
        NSDictionary *json=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
        NSLog(@"%@",json);
        marray=[NSMutableArray array];
        for (NSDictionary *dict in json) {

        }
        dispatch_async(dispatch_get_main_queue(), ^{  
            [self.tableView reloadData];
        });
    });

Is this the right way to handle data and reload the table in Objective C? If yes, then I still see some delay in seeing the data on tableview. Is there any way I can eliminate this delay? By the way this is second screen in my storyboard.

Pra Do
  • 293
  • 2
  • 3
  • 10
  • You seem to be doing everything right. How about changing your queue to `DISPATCH_QUEUE_CONCURRENT`? There's really no way I can see that you can avoid the delay because your `tableView` is waiting for that data to come in before it refreshes, and that can take a second. Developers might use a "loading" circle to show users that you're retrieving. You're doing it right by running your network tasks on the background thread and then dispatching UI refresh on the main thread. – rb612 Oct 23 '15 at 06:18
  • @rb612...I tried using concurrent queue but the result was same. – Pra Do Oct 23 '15 at 06:20
  • Well what you could do is upon clicking on the button in the previous view controller or at some point before the user segues to this VC, you can get the data and then pass it along from your first VC to your second VC. – rb612 Oct 23 '15 at 06:23

1 Answers1

0

You are doing it all right. Download the data on background thread and handing it over to table view to reload data in main thread is all what you need to do. It's almost certainly your own delay (the network delay and the parsing time) in providing the data to the table, not the UITabvleView's delay in handling your reloadData call.

Some of the general rules to be followed when doing this:

  1. Show loading overlay on screen when server call is going on.
  2. Once data is returned pass it on to table view on main thread. Remove the loading overlay now.
  3. Do not do heavy stuff in cellForRowAtIndexPath: method.

As a side note, although the same thingy but try once with below if you are following all above guidelines.

[self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
Abhinav
  • 37,684
  • 43
  • 191
  • 309