UITableView reloadData花费了太多时间 [英] UITableView reloadData taking too much time

查看:137
本文介绍了UITableView reloadData花费了太多时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用TBXML + HTTP从网站获取XML数据。我想要做的是用处理过的数据填充UITableView。
我创建了一个NSMutableArray,它包含所有条目,并且就此而言,一切正常。
问题是,在成功从服务器获取数据并将其存储在数组中之后,我尝试更新表以显示数据,使用 reloadData

I'm using TBXML+HTTP to get XML data from a website. What I want to do is populate a UITableView with the processed data. I've created a NSMutableArray that holds all the entries and, as far as that goes, everything is working fine. The problem is when, after successfully fetching the data from the server and storing it in the array, I try to update the table to show the data, using reloadData.

用20行填充表大约需要5秒钟。奇怪的是,提取速度非常快,因此可以立即获取数据。我不明白花了这么长时间。以下是日志中的一些信息:

It takes about 5 seconds to populate the table with 20 rows. The weird part is that the fetching is happening really fast, so the data is available right away. I do not understand what is taking so long. Here is some info from the log:

2012-03-17 18:46:01.045 MakeMyApp[4571:207] numberOfSectionsInTableView: 1
2012-03-17 18:46:01.047 MakeMyApp[4571:207] numberOfRowsInSection: 0
2012-03-17 18:46:01.244 MakeMyApp[4571:1f03] numberOfSectionsInTableView: 1
2012-03-17 18:46:01.245 MakeMyApp[4571:1f03] numberOfRowsInSection: 20
2012-03-17 18:46:01.245 MakeMyApp[4571:1f03] Ok, I'm done. 20 objects in the array.
2012-03-17 18:46:01.246 MakeMyApp[4571:1f03] Finished XML processing.
2012-03-17 18:46:06.197 MakeMyApp[4571:1f03] cellForRowAtIndexPath:

正如你所看到的,它触发了对 numberOfSectionsInTableView: / numberOfRowsInSection:两次:第一次是视图时加载,第二个是当我做 [self.tableView reloadData];

As you can see, it fires the pair numberOfSectionsInTableView:/numberOfRowsInSection: two times: the first is when the view loads, the second is when I do [self.tableView reloadData];

你看,小于第二,数组中填充了20个对象,并完成了处理XML的所有工作。如何 cellForRowAtIndexPath:仅在5秒后触发?

You see that, in less than a second, the array is populated with the 20 objects and all the work processing the XML is done. How is cellForRowAtIndexPath: fired only 5 seconds later?

以下是代码的某些部分可能有助于查找问题:

Here are some parts of the code that might help finding the problem:

- (void)createRequestFromXMLElement:(TBXMLElement *)element {
    // Let's extract all the information of the request
    int requestId = [[TBXML valueOfAttributeNamed:@"i" forElement:element] intValue];

    TBXMLElement *descriptionElement = [TBXML childElementNamed:@"d" parentElement:element];
    NSString *description = [TBXML textForElement:descriptionElement];

    TBXMLElement *statusElement = [TBXML childElementNamed:@"s" parentElement:element];
    int status = [[TBXML textForElement:statusElement] intValue];

    TBXMLElement *votesCountElement = [TBXML childElementNamed:@"v" parentElement:element];
    int votesCount = [[TBXML textForElement:votesCountElement] intValue];    

    // Creating the Request custom object
    Request *request = [[Request alloc] init];

    request.requestId = requestId;
    request.description = description;
    request.status = status;
    request.votes_count = votesCount;

    [requestsArray addObject:request];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    appListCell *cell = (appListCell *)[tableView dequeueReusableCellWithIdentifier:@"appListCell"];

    Request *request = [requestsArray objectAtIndex:indexPath.row];

    cell.appIdLabel.text = [NSString stringWithFormat:@"#%d", request.requestId];
    cell.appTextLabel.text = request.description;
    cell.appVotesLabel.text = [NSString stringWithFormat:@"%d votes", request.votes_count];

    return cell;
}

非常感谢!

编辑:

- (void)traverseXMLAppRequests:(TBXMLElement *)element {
    int requestCount = 0;
    TBXMLElement *requestElement = element->firstChild;

    // Do we have elements inside <re>?
    if ((requestElement = (element->firstChild))) {
        while (requestElement) {
            [self createRequestFromXMLElement:requestElement];

            requestCount++;

            requestElement = [TBXML nextSiblingNamed:@"r" searchFromElement:requestElement];
        }
    }

    [self.tableView reloadData];   

    NSLog(@"Ok, I'm done. %d objects in the array.", [requestsArray count]);

}

编辑2:我试过获取正确的信息1行,而不是20,延迟完全相同。

EDIT 2: I've tried fetching the information of just 1 row, instead of 20, and the delay is exactly the same.

推荐答案

我知道这是真的老问题,但我刚刚遇到完全相同的问题,并认为我可能知道原因。可能会帮助遇到同样问题的人...

I know this is a really old question, but I've just come across the exact same issue and think I might know the cause. Might help someone who stumbles across this same question...

我怀疑你的 reloadData 电话正在发生后台线程而不是主线程。你所描述的是这个的确切影响 - 代码运行但更新用户界面有很长的延迟(5-10秒)。

I suspect that your reloadData call is happening on a background thread as opposed to the main thread. What you describe is the exact effect of this - the code runs but there is a long delay (5-10 secs) in updating the user interface.

尝试更改 reloadData 致电:

dispatch_async(dispatch_get_main_queue(), ^{
    [self.tableView reloadData];
});

这篇关于UITableView reloadData花费了太多时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆