GCD缓慢更新UI

时间:2014-04-08 02:59:12

标签: ios objective-c grand-central-dispatch

我使用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];
}

1 个答案:

答案 0 :(得分:0)

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

- (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];         

   });
}