I have to download an image from the server but I do not wish to create NSURLConnection, I know that UIKit is not thread safe so I tried this, just need a confirmation whether if this is safe or can it result in a crash (as of now it is working fine.)I tried the following
Look at the case 2 of the switch.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
switch (indexPath.row) {
case 0:
{
CreateNewSurveyViewController *vc=[[CreateNewSurveyViewController alloc] init];
[self.navigationController pushViewController:vc animated:YES];
[vc release];
break;
}
case 1:{
MySurveyViewController *mySurveyViewController=[[MySurveyViewController alloc] init];
[self.navigationController pushViewController:mySurveyViewController animated:YES];
[mySurveyViewController release];
break;
}
case 2:{
self.progressHud.hidden = NO;
[self performSelectorInBackground:@selector(loadProfileImage) withObject:nil];
break;
}
default:
break;
}
}
-(void)loadProfileImage {
NSData* profileImageData = [NSData dataWithContentsOfURL:[NSURL URLWithString:self.userAccount.profileImageURL]];
[self performSelectorOnMainThread:@selector(launchSettingsView:) withObject:profileImageData waitUntilDone:YES];
}
-(void)launchSettingsView:(NSData*)profileImageData {
self.userAccount.userImage = [UIImage imageWithData:profileImageData];
self.progressHud.hidden = YES;
SettingsViewController* settingsViewController=[[SettingsViewController alloc] init];
settingsViewController.userAccount = self.userAccount;
settingsViewController.delegate = self;
[self.navigationController pushViewController:settingsViewController animated:YES];
[settingsViewController release];
}
This looks safe to me. You do all your networking on a background thread, and then only touch the UI in the main thread method you call back to. Usually people will use GCD or NSOperationQueue but this should work too.