简体   繁体   中英

NSURLSessionDataTask completion handler block is not called

Below is my code

      NSURL *serverUrl =[NSURL URLWithString:urlString];


NSURLSession *session=[NSURLSession sharedSession];
NSURLSessionDataTask *sessionTask =[session dataTaskWithURL:serverUrl completionHandler:^(NSData *data,NSURLResponse *response,NSError *errror){


    NSString *returnedString =[NSString stringWithUTF8String:[data bytes]];

    NSLog(@"returnedString %@",returnedString);


    NSError *parseError;

    NSArray *jsonArray=[NSJSONSerialization JSONObjectWithData:data options:0 error:&parseError];
    NSLog(@"jsonArray %@",jsonArray);


}];
[sessionTask resume];

Completion Handler block is not executed. If I keep breakpoint and check till NSURLSessionDatatask it works perfectly. Once it hits the completion Handler block then the breakpoint jumps to the [datatak resume]line. This is my problem.

completionHandler won't get executed immediately, just like how the name sounds it will be executed only after the task is completed.

So its normal behavior to not execute the completionHandler immediately and go to the next line.

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