I've this piece of code that try to feed an array from a web-service. If it fails, an error message appears, with the possibility to tap on it to retry. But when I tap on it, the error message doesn't disappear, and the spinner doesn't re-appear.
-(void)Load_Commandes{
if(LIB_Error)
{
[LIB_Error removeFromSuperview];
LIB_Error = nil;
}
TABLE_Commandes.hidden = YES;
LIB_Chargement.hidden = NO;
spinner.hidden = NO;
[spinner startAnimating];
tab_Cdes = [self Init_Tableau];
spinner.hidden = YES;
LIB_Chargement.hidden = YES;
if(tab_Cdes != nil)
{
TABLE_Commandes.hidden = NO;
[TABLE_Commandes reloadData];
}
else
{
LIB_Error = [[UILabel alloc] initWithFrame:self.view.frame];
[LIB_Error setTextColor:[UIColor grayColor]];
[LIB_Error setBackgroundColor:[UIColor clearColor]];
[LIB_Error setTextAlignment:NSTextAlignmentCenter];
[LIB_Error setText:@"Erreur de chargement. \nToucher pour réessayer."];
[LIB_Error setNumberOfLines:2];
UITapGestureRecognizer* gesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(Load_Commandes)];
[LIB_Error addGestureRecognizer:gesture];
LIB_Error.userInteractionEnabled = YES;
[self.view addSubview:LIB_Error];
}
}
Here is the code I use to call the web-service, in [self Init_Tableau].
NSString *str=[NSString stringWithFormat:URL_ORDERS];
NSURL *url=[NSURL URLWithString:str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error: &error];
Thanks you for your help, because I'll become crazy! ^^
UPDATE : I tried by a lot of different ways. But it seems to be impossible to do. Whatever I do, the visual changes are applied only when all treatments are done.
Here is the code of Init_Tableau.
-(NSArray*)Init_Tableau
{
NSMutableArray* a = [NSMutableArray array];
Reachability *networkReachability = [Reachability reachabilityForInternetConnection];
NetworkStatus networkStatus = [networkReachability currentReachabilityStatus];
if (networkStatus == NotReachable) {
NSLog(@"Erreur de chargement 1");
//return nil;
}
NSURLResponse* response;
NSError* error;
NSString *str=[NSString stringWithFormat:URL_ORDERS];
NSURL *url=[NSURL URLWithString:str];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:5.0];
NSData *returnData = [NSURLConnection sendSynchronousRequest: request returningResponse:&response error: &error];
NSString *responseBody = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
if(error)
{
NSLog(@"Erreur de chargement 2");
return nil;
}
NSError *e = nil;
NSArray* resultList = [NSJSONSerialization JSONObjectWithData:returnData options:NSJSONReadingMutableContainers error:&e];
for(NSDictionary* dico in resultList){
Entete* cde = [[Entete alloc] init];
cde.ncde = [dico objectForKey:NCDE];
cde.nomf = [dico objectForKey:NOMF];
cde.noml = [dico objectForKey:NOML];
cde.prenomf = [dico objectForKey:PRENOMF];
cde.prenoml = [dico objectForKey:PRENOML];
cde.adresse1f = [dico objectForKey:ADRESSE1F];
cde.adresse1l = [dico objectForKey:ADRESSE1L];
cde.adresse2f = [dico objectForKey:ADRESSE2F];
cde.adresse2l = [dico objectForKey:ADRESSE2L];
cde.cpf = [dico objectForKey:CPF];
cde.cpl = [dico objectForKey:CPL];
cde.villef = [dico objectForKey:VILLEF];
cde.villel = [dico objectForKey:VILLEL];
cde.phonef = [dico objectForKey:PHONEF];
cde.phonel = [dico objectForKey:PHONEL];
cde.societef = [dico objectForKey:SOCIETEF];
cde.societel = [dico objectForKey:SOCIETEL];
cde.etatCde = [dico objectForKey:ETATCDE];
cde.moyenPaiement = [dico objectForKey:MOYENPAIEMENT];
cde.fdp = [dico objectForKey:FDP];
cde.mnt = [[dico objectForKey:MNTCDE] floatValue];
[a addObject:cde];
}
return [NSArray arrayWithArray:a];
}