0

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];
    }
Brad Larson
  • 170,088
  • 45
  • 397
  • 571
Verdant
  • 288
  • 1
  • 10
  • 1
    Why not just hide the UILabel? Seems easier to me. – ArtOfWarfare Oct 12 '14 at 19:06
  • I tried, it doesn't work. When I try to reload, nothing of these instructions work. LIB_Error.hidden = YES spinner.hidden = NO LIB_Chargement.hidden = NO – Verdant Oct 12 '14 at 19:14
  • That would be because you're not addressing the right objects. Eg, is `LIB_Error` even set? – Hot Licks Oct 12 '14 at 19:20
  • Possible duplicate of [removeFromSuperview not working](http://stackoverflow.com/q/25413666/608639) – jww Oct 12 '14 at 19:26
  • I don't understand what you mean. But, of course, LIB_Error is set. The first time i run Load_Commande, LIB_Error = nil. If the Init_Tableau method returns nil, the LIB_Error appears, with a GestureRecognizer to call the Load_Commandes method. – Verdant Oct 12 '14 at 19:27
  • You need to put your code in context. Is all the code you show in the same class? How is Load_Commandes called? It would be helpful to see the whole Init_Tableau method also. – rdelmar Oct 12 '14 at 20:02
  • @Atomnium How do you know? Do you have any evidence that `removeFromSuperview` is even getting called? You haven't mentioned stepping through this with a debugger and you don't have any log method inside that `if` statement. – ArtOfWarfare Oct 12 '14 at 20:03
  • @ArtOfWarfare I used a breakpoint, and used NsLog to see the value of LIB_Error, wasn't nil. – Verdant Oct 12 '14 at 20:09
  • @rdelmar Load_Commande is called in the ViewDidAppear method of my viewController. I'll post the entire code of Init_tableau. And what do you mean by a context? Thx :) – Verdant Oct 12 '14 at 20:11
  • I think your problem is that you are issuing the data fetch using a synchronous method on the main thread - blocking it, so the remove from superview doesn't get actioned until after it has been re-added. You need to dispatch the fetch onto a background thread – Paulw11 Oct 12 '14 at 20:19
  • "But, of course, (some value) is set." I wish I had money for every time someone has made that claim here, only to have it proved false. Don't say "of course" unless you have added an NSLog at the point where you did `LIB_Error.hidden = YES` to prove that its set. – Hot Licks Oct 12 '14 at 22:32
  • Excuse me but I'm developer (not on xCode, okay) then I know a little bit what to do before ask something ;) Then, "of course", the object was set. I tried to NSLog and all before posting. I found a dirty way to do what I want by using a method to hide LIB_Error and hide=NO the spinner and after this, call my Load method with a NSTimer. I don't like it but it's the only way I found. – Verdant Oct 12 '14 at 23:33
  • Suit yourself. But don't claim that "feature xxxx doesn't work" when the fault is most certainly in your code and not in the OS. – Hot Licks Oct 13 '14 at 00:24

0 Answers0