문제

Im using GCD to download a plist file from the internet then enter the data into an array then load it into a tableView.

The data is being downloaded and converted alright but the table view is taking ages to update. I have the app Logging something when it calls [self.tableView reloadData] but the table view updates about 10 seconds later.

I don't think this is about the table view because I tried changing the title of the Navigation Bar inside the GCD and there was also a delay. So it might be a delay updating the user interface?

This is my GCD code:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

Thanks for you help

EDIT:

This is my code:

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

Edit 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];
}
도움이 되었습니까?

해결책

i think following code will solve your problem ...

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

   });
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top