How to make MainThread wait until any async action is performed?

I have UITableViewwith some content that loads asynchronously . When the user rotates the device, I need to do [tableView reloadData]in -willRotateToInterfaceOrientation. ReloadData works asynchronously in my case.

I understand that reloadData works in the main thread, but it runs cellForRowAtIndexPath, which works async in my case.

So the question is how to make the main thread to the end UITableView reloadData.

+5
source share
3 answers

reloadData willRotateToInterfaceOrientation, . UIViews ( - - reloadData ).

, , "" . , willRotateToInterfaceOrientation, . ( "" ).

Apple NSRunLoop. , iOS, , .

https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html

+2
+3

, reloadData UITableView.

GCD, . , , [tableView reloadData]. :

- (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration{

  ...
  dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
  dispatch_async(queue, ^{

     // load your table data here
     [self loadMyTableData];

     // when done dispatch back to the main queue to load the table
     dispatch_queue_t mainQueue = dispatch_get_main_queue();
     dispatch_async(mainQueue, ^{

        [self.tableView reloadData];
      });
  });
  ...
}
+2

All Articles