GCD 缓慢更新 UI



我使用GCD从互联网下载plist文件,然后将数据输入数组,然后将其加载到tableView中。

数据正在下载和转换,但表视图需要很长时间才能更新。我有应用程序在调用 [self.tableView reloadData] 时记录一些东西,但表视图在大约 10 秒后更新。

我不认为这与表格视图有关,因为我尝试更改 GCD 内导航栏的标题,并且也有延迟。所以这可能是更新用户界面的延迟?

这是我的 GCD 代码:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

谢谢你的帮助

编辑:

这是我的代码:

 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
 UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"url to file"]];
NSLog(@"Array: %@", UERootArray);
//NSLog(@"count %@",count);
NSString *currentName;
NSString *currentLoaction;
NSString *currentDate;
int currentEventInt = 0;
NSArray *currentEventArr = [UERootArray objectAtIndex:0];
while (currentEventArr.count > currentEventInt)
{
    currentName = [currentEventArr objectAtIndex:0];
    currentLoaction = [currentEventArr objectAtIndex:1];
    currentDate = @"7pm 17/11"; //[currentEventArr objectAtIndex:2];
    NSLog (@"Title: %@", currentName);
    NSLog (@"News: %@", currentLoaction);
    NSLog (@"Date: %@", currentDate);
    UEEvent *currentTask = [[UEEvent alloc] initWithName:currentName location:currentLoaction date:currentDate];
    [self.upcommingEvents addObject:currentTask];
    currentEventInt = currentEventInt + 1;
}
UEDownloaded = YES;
[self.tableView reloadData];

编辑 2:

- (void)downloadUEData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"url to file"]];
        [self processUEData];
    });
}
- (void)processUEData
{
    NSLog(@"Array: %@", UERootArray);
    //NSLog(@"count %@",count);
    NSString *currentName;
    NSString *currentLoaction;
    NSString *currentDate;
    int currentEventInt = 0;
    NSArray *currentEventArr = [UERootArray objectAtIndex:0];
    while (currentEventArr.count > currentEventInt)
    {
        currentName = [currentEventArr objectAtIndex:0];
        currentLoaction = [currentEventArr objectAtIndex:1];
        currentDate = @"7pm 17/11"; //[currentEventArr objectAtIndex:2];
        NSLog (@"Title: %@", currentName);
        NSLog (@"News: %@", currentLoaction);
        NSLog (@"Date: %@", currentDate);
        UEEvent *currentTask = [[UEEvent alloc] initWithName:currentName location:currentLoaction date:currentDate];
        [self.upcommingEvents addObject:currentTask];
        currentEventInt = currentEventInt + 1;
    }
    UEDownloaded = YES;
    [self.tableView reloadData];
}

我认为以下代码将解决您的问题...

- (void)downloadUEData
{
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
    UERootArray = [[NSArray alloc] initWithContentsOfURL:[NSURL URLWithString:@"http://www.nytrex.webs.com/Linx/upcommingEvents.plist"]];
        [self processUEData];
    });
}
- (void)processUEData
{
    NSLog(@"Array: %@", UERootArray);
    //NSLog(@"count %@",count);
    NSString *currentName;
    NSString *currentLoaction;
    NSString *currentDate;
    int currentEventInt = 0;
    NSArray *currentEventArr = [UERootArray objectAtIndex:0];
    while (currentEventArr.count > currentEventInt)
    {
        currentName = [currentEventArr objectAtIndex:0];
        currentLoaction = [currentEventArr objectAtIndex:1];
        currentDate = @"7pm 17/11"; //[currentEventArr objectAtIndex:2];
        NSLog (@"Title: %@", currentName);
        NSLog (@"News: %@", currentLoaction);
        NSLog (@"Date: %@", currentDate);
        UEEvent *currentTask = [[UEEvent alloc] initWithName:currentName location:currentLoaction date:currentDate];
        [self.upcommingEvents addObject:currentTask];
        currentEventInt = currentEventInt + 1;
    }
    UEDownloaded = YES;
    dispatch_async(dispatch_get_main_queue(), ^{   // update your UI in main thread
       [self.tableView reloadData];         
   });
}

最新更新