Table View takes a lot of time to refresh after “reloadData()”

后端 未结 2 1603
终归单人心
终归单人心 2021-02-10 12:06

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 \"

相关标签:
2条回答
  • 2021-02-10 12:13

    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();
      }
    
    0 讨论(0)
  • 2021-02-10 12:33

    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.

    0 讨论(0)
提交回复
热议问题