在NSURLSession中使用时,UIAlertView需要太长时间

Fer*_*ago 0 uialertview ios nsurlsession ios8 nsurlsessiondatatask

NSURLSession用来从我的服务器获取一些信息.在我获得数据后,我将其显示在a中UIAlertView,但显示时间太长.我用NSLog它打印数据,它几乎立即打印出来......那么发生了什么?该方法dataTaskWithRequest不是异步的吗?为什么需要这么多时间来弹出警报视图?

NSURLSession *session;
session = [NSURLSession sharedSession];
NSURL * url = [NSURL URLWithString:[DRESSABLE_IP stringByAppendingString:@"index.php"]];
NSMutableURLRequest * urlRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:CONNECTION_CACHEPOLICY  timeoutInterval:CONNECTION_TIMEOUT];
[urlRequest setHTTPMethod:@"POST"];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest 
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString *total = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
        [[[UIAlertView alloc] initWithTitle:@"Title" message:total delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
 }];
Run Code Online (Sandbox Code Playgroud)

Jar*_*rod 7

所有UI活动必须在主队列中完成.

NSURLSession *session;
session = [NSURLSession sharedSession];
NSURL * url = [NSURL URLWithString:[DRESSABLE_IP stringByAppendingString:@"index.php"]];
NSMutableURLRequest * urlRequest = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:CONNECTION_CACHEPOLICY  timeoutInterval:CONNECTION_TIMEOUT];
[urlRequest setHTTPMethod:@"POST"];
NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:urlRequest 
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSString *total = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(),^{
        [[[UIAlertView alloc] initWithTitle:@"Title" message:total delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show];
});
 }];
Run Code Online (Sandbox Code Playgroud)