This is what Im tryin开发者_JAVA百科g to do: Get a xml from apple's RSS containing info about apps, parse it, and display the apps in an UITable view.
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.
here is the project: http://www.mediafire.com/?2dn6g916bh9uf4v
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.
Edit: some more information, and to address the delay:
The code calls UI routines ("[table reloadData]") from outside the main thread. That's bad, don't do that.
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.
EDIT: 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];
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.
I was also facing same problem. I solved this by running my code on UI thread as :
NSOperationQueue.mainQueue().addOperationWithBlock(){
//Perform action on main thread
tableView.reloadData();
}
精彩评论