In the viewDidLoad() method of view controller I am calling
[NSThread detachNewThreadSelector:@selector(readFromFaceBook)
toTarget:self
withObject:nil];
readFromFaceBook will pull the data from remote location and populates the data in NSMutabaleArray called “myData”. The view controller contains a tableView which loads data from myData.
The problem I have is, how to delay the execution of tableView delegates until all the data is loaded into “myData” by the thread ..?? The program is crashing as delegate functions are trying to execute using myData . Any help is greatly appreciated…!!!!
You should create the tableview and show it immediately, so the user doesn’t think your app is non-functional. Perhaps a
UIProgressIndicatoror something to show that you’re doing something?Your background thread should be doing the loading, as you’ve correctly chosen. When it’s done, call back to the main thread via something like
performSelectorOnMainThread:...ordispatch_async()to thedispatch_get_main_queue(), and invoke[myTableView reloadData]to refresh the UI.To prevent the tableview from loading prematurely, there are several approaches you could take. Presumably you’re storing your data in an array, since a
UITableViewdisplays ordered data. If so, then just don’t populate the array until the thread finishes. If that doesn’t work, you could just have aBOOLivar that you use to indicate whether you’ve finished loading, then use the state of theBOOLto determine what you should be showing in yourUITableView.