0

I am trying to get my tableview to update via this async code

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^(void)
{

   NSString *mainURL = @"http://myurl.com/api/";
   NSString *firstprefix = @"type=topdeals&device=";
   NSString *deviceIDforURL = [NSString stringWithFormat:@"%@", Devicetoken];
   NSString *stringToGoToEncoder = [NSString stringWithFormat: @"%@%@", firstprefix, deviceIDforURL];
   NSData *plainData = [stringToGoToEncoder dataUsingEncoding:NSUTF8StringEncoding];
   NSString *base64String = [plainData base64EncodedStringWithOptions:0];
   NSString *returnURL = [NSString stringWithFormat:@"%@%@", mainURL, base64String];
   NSURL *returncompletedURL = [[NSURL alloc] initWithString:returnURL];
  //NSURL *request = [NSURL URLWithString:returncompletedURL];

   NSData *data = [NSData dataWithContentsOfURL:returncompletedURL];
   NSError *error;
   NSMutableDictionary *responseObject = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error: &error];

   NSLog(@"%@", responseObject);

   allLogos = [[responseObject valueForKey:@"data"] valueForKey:@"logo"];
   allcontent = [[responseObject valueForKey:@"data"] valueForKey:@"content"];
   allpostode = [[responseObject valueForKey:@"data"] valueForKey:@"postcode"];

   allname = [[responseObject valueForKey:@"data"] valueForKey:@"name"];
   alladdress = [[responseObject valueForKey:@"data"] valueForKey:@"address"];
   alladdress2 = [[responseObject valueForKey:@"data"] valueForKey:@"address2"];
   alllat = [[responseObject valueForKey:@"data"] valueForKey:@"lat"];
   alllong = [[responseObject valueForKey:@"data"] valueForKey:@"lng"];
   allstart = [[responseObject valueForKey:@"data"] valueForKey:@"start"];
   allfinish = [[responseObject valueForKey:@"data"] valueForKey:@"finish"];
   allstartnice = [[responseObject valueForKey:@"data"] valueForKey:@"nicestart"];
   allfinishnice = [[responseObject valueForKey:@"data"] valueForKey:@"nicefinish"];



   dispatch_sync(dispatch_get_main_queue(), ^(void)
                  {
                      NSLog(@"%@", allcontent);
                      [self.tableview reloadData];
                  });
});

My table view is not updating via this command.

Here's my .h file

@interface ViewController : UIViewController <UITableViewDelegate, UITableViewDataSource, UIPickerViewDelegate, CLLocationManagerDelegate>
{
    CLLocationManager *locationManager;

}
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *spinner;
@property (weak, nonatomic) IBOutlet UITableView *tableview;
@property (weak, nonatomic) IBOutlet UILabel *LocationLabel;
@property (weak, nonatomic) IBOutlet UIButton *SettingsButton;
@property (weak, nonatomic) IBOutlet UISwitch *OutSwitch;
@property (weak, nonatomic) IBOutlet UILabel *LatestDealsLabel;

@property (weak, nonatomic) IBOutlet UILabel *Blackout;

my table view datasource is "allcontent" which is a global NSArray. I can see in the log that "allcontent" is updated with the correct content before updating the tableview.

Any help much appreciated.

HRM
  • 2,097
  • 6
  • 23
  • 37
  • It worked before you added the GCD blocks? Are any table delegate / data source methods called? – Wain Mar 18 '14 at 12:33
  • It needs to run using GCD blocks. All the datasource are in my .h file @interface. – Grant Carlisle Mar 18 '14 at 12:36
  • 1
    That doesn't answer either of my questions. Have you ever seen it work? What did you change after that? Did you debug? Having something defined in the .h file doesn't mean it's connected to anything... – Wain Mar 18 '14 at 12:39
  • The tableview does work as on the first run it reloads the table view however after that it doesn't reload anymore. – Grant Carlisle Mar 18 '14 at 12:41
  • 1
    And what's different between the first and second runs? – Wain Mar 18 '14 at 12:46
  • the first time it runs a function containing the async above,when a lat/lng is received . The second time it runs it when a picker view is selected. – Grant Carlisle Mar 18 '14 at 12:48

3 Answers3

0

Do not call the dispatch_sync function if you are executing task on the same queue it will going to deadlock mode.

so instead of that

    dispatch_sync(dispatch_get_main_queue(), ^(void)
                                          {
     //update UI in main thread     

    });

this link is helpful you

Why can't we use a dispatch_sync on the current queue?

Community
  • 1
  • 1
codercat
  • 22,873
  • 9
  • 61
  • 85
0

Silly mistake my end

Was running the function without using self so wasn't running the function and hence self tableview reloadData; wasn't reloading the correct tableview.

Thanks anyway

0

Ideally you would wan to create a queue when using dispatch_async to download table data

dispatch_queue_t table_download_queue = dispatch_queue_create("com.mymobile.indetifier", NULL);

 dispatch_async(table_download_queue, ^{

// fill your data source array from the data you fetch from web service API.

                dispatch_async(dispatch_get_main_queue(), ^{
                    [self.tableView reloadData];
                });
            }); 
AppleDelegate
  • 4,269
  • 1
  • 20
  • 27