Search code examples
objective-cuitableviewreloaddata

UITableView reload data takes forever


I parse a couple of things via JSON into a table view but I have the problem that the parsing takes about a second (I know this due to the network indicator) but then there is a huge delay until the data appears in the table view.

I tried to place [tableView reloadData]; at a couple of places already but no success.

Here is my code.

I have defined mainThreadQueue and myClassicoAPI as a macro.

- (void)viewDidLoad
{
    [super viewDidLoad];

    arrayNeuheiten = [[NSArray alloc] init];
    arrayArtikelName = [[NSArray alloc] init];
    dictionaryNewStuff = [[NSDictionary alloc] init];

    [self parseJSONWithURL:myClassicoAPI];
    //[self performSelector:@selector(updateTableView) withObject:nil afterDelay:NO];

    [neuheietenTable reloadData];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];

    [neuheietenTable reloadData];
}

-(void) updateTableView {
    [neuheietenTable reloadData];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)numberOfRowsInSection:(NSInteger)section {

    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    //return (self.arrayNeuheiten.count<17)?self.arrayNeuheiten.count : 17;

    return MIN(18, arrayNeuheiten.count);

}



-(void) parseJSONWithURL: (NSURL *) jsonURL {

    dispatch_async(mainThreadQueue, ^{
        NSError *error = nil;

        [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;

        NSString *json =[NSString stringWithContentsOfURL:jsonURL encoding:NSJSONWritingPrettyPrinted error:&error];

        if (error == nil) {


            NSData *jsonData = [json dataUsingEncoding:NSJSONWritingPrettyPrinted];

            dictionaryNewStuff = [NSJSONSerialization JSONObjectWithData:jsonData options:kNilOptions error:&error];
            if (error == nil) {
                dispatch_async(mainThreadQueue, ^{
                    arrayArtikelName = [[dictionaryNewStuff valueForKey:@"newstuff"] valueForKey:@"Neuheiten"];
                    arrayNeuheiten = [[dictionaryNewStuff valueForKey:@"newstuff"] valueForKey:@"Neuheiten"];
                    [neuheietenTable reloadData];
                    [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
                });
            } else {
                nil;
            }

        } else {
            nil;
        }
    });

}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"NeuheitenCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];

    if (cell == nil)
    {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier];
    }

    cell.textLabel.text = [[arrayArtikelName objectAtIndex:indexPath.row] objectForKey:@"name"];

    return cell;
}

Thanks in advance

Constantin


Solution

  • You're loading the data synchronous on the mainthread which is bad and blocking the interface. Try loading your JSON data with NSURLConnection and reload your UITableView in the

    (void)connectionDidFinishLoading:(NSURLConnection *)aConnection
    

    method when you're done processing it.