5

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.

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

AmitK
  • 75
  • 1
  • 6

2 Answers2

20

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.

Bogatyr
  • 19,255
  • 7
  • 59
  • 72
  • I put some NSLog in all his methods (including his parsing, and his nsoperations). And after the last method fired there is a delay of 5 seconds until the [table reloadData] from this method triggers `tableView:cellForRowAtIndexPath:`. – Matthias Bauch Feb 20 '11 at 13:19
  • +1 I totally missed the not on main thread part. I read every line 10 times and couldn't figure it out. What a shame ^^ – Matthias Bauch Feb 22 '11 at 07:51
  • Hi I have added reload data in main thread itself, but still its taking time to reload table.. – RashmiG Jul 29 '16 at 09:48
1

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();
  }
Uniruddh
  • 4,427
  • 3
  • 52
  • 86