1

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...!!!!

3 Answers 3

1

You should create the tableview and show it immediately, so the user doesn't think your app is non-functional. Perhaps a UIProgressIndicator or 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:... or dispatch_async() to the dispatch_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 UITableView displays ordered data. If so, then just don't populate the array until the thread finishes. If that doesn't work, you could just have a BOOL ivar that you use to indicate whether you've finished loading, then use the state of the BOOL to determine what you should be showing in your UITableView.

0
1

Two things you can do: either instantiate your tableView after you get the data or call [tableView reloadData] after you get a callback saying that your data is done loading.

1
  • ..after you get a callback saying that your data is done loading.." can show me sample code to accomplish this. Also how can I initiate tableView after getting the data...??
    – bp581
    Jan 24, 2011 at 4:01
0

Your secondary thread should not be handling UI work.

Once you get call back on download is done and data is in "myData" collection --- call a function in Main thread context let say you are calling UpdateTable:.

Use following API to do this

[self performSelectorOnMainThread:@selectore(UpdateTable:) withObject:<#(id)arg#> waitUntilDone:<#(BOOL)wait#>

IN the UpdateTable

- (void)UpdateTable:(id)inData
{
    [<view object> reloadData];
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.