简体   繁体   中英

Objective-C dataTaskWithRequest populate variable Synchronous

I have trouble with dataTaskWithRequest, I am trying to populate a variable when I call a method that is using dataTaskWithRequest. but it appears not populate because when I call this variable later, Its nil

Here is the method:

-(void)deviceCheck:(NSString *)device Completetion:(void (^) (NSArray * result,NSError * error))completion{
    NSString *deviceRequestString = [NSString stringWithFormat:@"%@?device=%@",webservice,device];
    NSURL *JSONURL = [NSURL URLWithString:deviceRequestString];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
    NSURLSessionDataTask * dataTask = [[NSURLSession sharedSession] dataTaskWithRequest:request
                                                                      completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                                                          if(data == nil){
                                                                              completion(nil,error);
                                                                              return;
                                                                          }
                                                                          NSError *myError;
                                                                          NSArray *tableArray = [[NSArray alloc]initWithArray:[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&myError]];
                                                                          completion(tableArray,myError);
                                                                      }];
    [dataTask resume];
}

and this is how I am calling it:

[self deviceCheck:@"testunitid" Completetion:^(NSArray *result, NSError *error) {

                        if([result count] == 0){

                            UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message" message:@"Device is not valid." preferredStyle:UIAlertControllerStyleAlert];

                            UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                            [alertController addAction:ok];

                            [self presentViewController:alertController animated:YES completion:nil];

                        }else{

                            scanner=[DTDevices sharedDevice];
                            [scanner addDelegate:self];
                            [scanner connect];

                            Name = [[result objectAtIndex:0] objectForKey:@"name"];

                            Name = [Name stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];

                            Name = [Name stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];

                        }


                    }];

I am trying to populate the variable Name When I put break points everywhere I can see its being populated, same with results, but when I try to call it here:

[self getLocationInfo:Name Completetion:^(NSArray *result, NSError *error) {

            if([result count] == 0){

                UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Message" message:@"There is an issue with the location tag." preferredStyle:UIAlertControllerStyleAlert];

                UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
                [alertController addAction:ok];

                [self presentViewController:alertController animated:YES completion:nil];

                ScannerMessage.text = @"READY TO SCAN";
                ScannerMessage.backgroundColor = [UIColor greenColor];

            }else{

                [self performSegueWithIdentifier: @"SegueIdentifier" sender: self];

            }

        }];

Its nil

Is there a better way to use dataTaskWithRequest ? I find this way a bit slow when I have multiple calls. Or is there away to use the variable Name later? I am very confused. I really miss sendSynchronousRequest I found that faster and better.

To avoid nil make sure you initialize the variable in both if else blocks:

if([result count] == 0){
   Name = @"[result count] == 0";
}else{
   //populate names
}

If [result count] == 0 then you have to do the debugging in that direction.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM