UITableView不在dispatch_async中更新

时间:2014-03-18 12:31:25

标签: ios objective-c uitableview asynchronous objective-c-blocks

我试图让我的tableview通过这个异步代码进行更新

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];
                  });
});

我的表格视图未通过此命令更新。

这是我的.h文件

@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;

我的表格视图数据源是&#34; allcontent&#34;这是一个全球的NSArray。我可以在日志中看到&#34; allcontent&#34;在更新tableview之前使用正确的内容进行更新。

任何帮助非常感谢。

3 个答案:

答案 0 :(得分:0)

如果您在同一队列上执行任务,它将进入死锁模式,请不要调用dispatch_sync函数。

所以而不是那个

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

    });

此链接对您有所帮助

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

答案 1 :(得分:0)

愚蠢的错误我的结局

在没有使用self的情况下运行该函数所以没有运行该函数,因此自我tableview reloadData;没有重新加载正确的tableview。

非常感谢

答案 2 :(得分:0)

理想情况下,您需要在使用dispatch_async下载表数据时创建队列

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];
                });
            });