在“reloadData()”之后,表视图需要花费大量时间才能刷新 [英] Table View takes a lot of time to refresh after "reloadData()"

查看:98
本文介绍了在“reloadData()”之后,表视图需要花费大量时间才能刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这就是我想要做的事情:
从apple的RSS中获取一个xml,其中包含有关应用程序的信息,解析它,并在UITable视图中显示应用程序。

This is what Im trying to do: Get a xml from apple's RSS containing info about apps, parse it, and display the apps in an UITable view.

问题是在reloadData()之后,显示信息大约需要5秒钟。
此外,应用程序在打开项目后第一次运行时总是崩溃。

The problem is that after the "reloadData()" it takes about 5 seconds until the info displayed. Also, the app always crashes the first time it run after opening the project.

这里是项目:
http://www.mediafire.com/?2dn6g916bh9uf4v

推荐答案

在表中显示缓慢加载(即基于网络)数据的公认惯用法是在单元格中显示活动指示符(如果数据尚未存在于表中),则下载后台的数据(NSOperationQueue和NSOperation对此非常有用),并在数据准备就绪时发出通知。几乎可以肯定,在向表提供数据时,你自己的延迟(网络延迟和解析时间),而不是UITabvleView在处理reloadData调用时的延迟。

The accepted idiom for displaying slow-loading (i.e., network-based) data in a table is to show an activity indicator in the cell if the data is not already present in the table, then download the data in the background (NSOperationQueue and NSOperation are great for this), and send out a notification when the data becomes ready. It's almost certainly your own delay (the network delay and the parsing time) in providing the data to the table, not the UITabvleView's delay in handling your reloadData call.

编辑:更多信息,并解决延迟:

some more information, and to address the delay:


  • 代码从外部调用UI例程([table reloadData])主线程。这很糟糕,不要这样做。

  • The code calls UI routines ("[table reloadData]") from outside the main thread. That's bad, don't do that.

你试图将图像信息推送到[self loadImages]中的表格而不是让表格在cellForRowAtIndexPath中一次拉一行信息。

You're trying to "push" image information into the table in [self loadImages] instead of letting the table "pull" information a row at a time in cellForRowAtIndexPath.

编辑:好的,把我的钱放在哪里我的嘴是:我做了以下更改,所有延迟都消失了(根据NSLog时间戳,它在performSelectorOnMainThread和cellForRowAtIndexPath之间约为2毫秒)。

OK, putting my money where my mouth is: I made the following changes and all delays are gone (according to the NSLog timestamps it is about 2 milliseconds between the performSelectorOnMainThread and cellForRowAtIndexPath).

//[table reloadData];
NSLog( @"thread: calling reload" );
[table performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:YES];
//[self loadImages];

注意,该应用程序仍然不正确,图像尚未刷新。请记住:在后台下载图像,并向视图控制器类发送新图像可用的通知,然后调用[重新加载表](当然,从主线程中运行的通知处理程序!)。不要从任何后台线程调用UIKit函数。这应该足以让你重构代码以做正确的事情。

Note, the app's still not right, the images don't refresh yet. Just remember: download the images in the background, and send a notification to your view controller class that new images are available, then call [reload Table] (from the notification handler running in the main thread, of course!). Don't call UIKit functions from any background thread. That should be enough for you to restructure the code to do the right thing.

这篇关于在“reloadData()”之后,表视图需要花费大量时间才能刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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