iosobjective-cxcodesegue

iOS objective c perform selector not executing the method


hey actually I used perform selector in a url request block and it was working fine at first but now it is not executing the function

the perform selector function is

       NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
        NSLog(@"requestReply: %@", jsonDict);
        self.tmp=[jsonDict valueForKey:@"otp"] ;
        self.str=self.tmp;
        NSLog(@"tmp storage inside block:%@",self.tmp);
      //  [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(updateStatus) userInfo:nil repeats:YES];
        [self performSelector:@selector(updateStatus) withObject:self afterDelay:1.0];
    }] resume];
}

and the update status method is

-(void)updateStatus{
NSLog(@" storage:%@",self.str);
NSLog(@"tmp storage:%@",self.tmp);
[ self performSegueWithIdentifier:@"b1" sender:self];
}

as it is not executing I am not able to perform segue and the app stays in the same page


Solution

  • simple , update your UI in main thread

      dispatch_async(dispatch_get_main_queue(), ^{
            [self performSelector:@selector(updateStatus) withObject:nil afterDelay:0.1];
             });
    

    option 2

    or else create the simple method

           NSLog(@"tmp storage inside block:%@",self.tmp);
            [self updateStatus];
    

    and call the method in Main thread

    -(void)updateStatus{
    NSLog(@" storage:%@",self.str);
    NSLog(@"tmp storage:%@",self.tmp);
    dispatch_async(dispatch_get_main_queue(), ^{
        [ self performSegueWithIdentifier:@"b1" sender:self];
    });
    }
    

    updated code

     NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
        [[session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
           // NSString *requestReply = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding]; // this is json string
            //   NSError *error;
            NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:&error]; // you need to convert to dictionary object
            NSLog(@"requestReply: %@", jsonDict);
            self.tmp=[jsonDict valueForKey:@"otp"] ;
            self.str=self.tmp;
            NSLog(@"tmp storage inside block:%@",self.tmp);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self performSelector:@selector(updateStatus) withObject:nil afterDelay:0.1];
            });
    
        }] resume];
    

    and call the method as

     -(void)updateStatus{
    NSLog(@" storage:%@",self.str);
    NSLog(@"tmp storage:%@",self.tmp);
    [ self performSegueWithIdentifier:@"b1" sender:self];
    
    
    }