I have a UITableview with a navigation bar on the top. I have a refresh button as the rightBarButtonItem.
When the refresh button is clicked i want to hide the refresh button, reload the table and display an alertview.
-(void)refreshClicked{
self.navigationItem.rightBarButtonItem=nil;
app.networkActivityIndicatorVisible = YES;
[appDelegate readJSONData];
[self.tableView reloadData];
UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Kampanjerna är nu uppdaterade" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[infoAlert show];
[infoAlert release];
}
I find that when my wifi signal gets weaker, the refresh button is not hidden immediately and there is a delay. I am afraid if 3G is used there will be further delays and the user might press the refresh button again, thinking first time it wasnt pressed.
IS there some problem with me code ?
Help would be appreciated
EDIT———–
-(void)refreshClicked{
self.navigationItem.rightBarButtonItem=nil;
app.networkActivityIndicatorVisible = YES;
// do data processing in the background
[self performSelectorInBackground:@selector(doBackgroundProcessing) withObject:self];
UIAlertView *infoAlert = [[UIAlertView alloc] initWithTitle:@"" message:@"Kampanjerna är nu uppdaterade" delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[infoAlert show];
[infoAlert release];
}
- (void)doBackgroundProcessing {
NSAutoreleasePool*pool=[[NSAutoreleasePool alloc] init];
[appDelegate readJSONData];
// must update the UI from the main thread only; equivalent to [self.tableView reloadData];
[self performSelectorOnMainThread:@selector(reloadData) withObject:self.tableView waitUntilDone:NO];
[pool release];
}
Error
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[campaignTableViewController reloadData]: unrecognized selector sent to instance 0x703eba0'
Basically, although you nil
rightBarButtonItem, that change won’t be reflected in the UI until control returns to the application’s main run loop. So if the rest of your method takes some noticeable time (like it would with network requests), you won’t see the button go away until after that work is done.More directly: you’re blocking the main thread; to fix it, you need to do the time-consuming work on a background thread.
Something like this ought to work (neither compiled nor tested):