简体   繁体   English

未调用NSURLSessionDataTask完成处理程序块

[英]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. 如果我保留断点并检查直到NSURLSessionDatatask,它会完美地工作。 Once it hits the completion Handler block then the breakpoint jumps to the [datatak resume]line. 一旦到达完成处理程序块,则断点将跳至[datatak resume]行。 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. completeHandler不会立即执行,就像名称听起来一样,仅在任务完成后才执行。

So its normal behavior to not execute the completionHandler immediately and go to the next line. 因此,它的正常行为是不立即执行completionHandler并转到下一行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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