美文网首页
iOS9.0 NSURLSession 网络请求,出现的错误解决

iOS9.0 NSURLSession 网络请求,出现的错误解决

作者: 文刂丽 | 来源:发表于2016-03-17 14:56 被阅读668次

- (void)getNetTime {

        NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];

        NSURLRequest *request=[NSURLRequest requestWithURL:url];

        NSURLSession *session = [NSURLSession sharedSession];

        NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request

        completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                         // do something with the data

         }];

        [dataTask resume];

}

运行的时候虽然不会崩溃,但是会报如下错误:This application is modifying the autolayout engine from a background thread, which can lead to engine corruption and weird crashes.  This will cause an exception in a future release.

解决方法:

- (void)getNetTime {

               NSURL *url=[NSURL URLWithString:@"http://www.baidu.com"];

               NSURLRequest *request=[NSURLRequest requestWithURL:url];

               NSURLSession *session = [NSURLSession sharedSession];

               NSURLSessionDataTask *dataTask = [session dataTaskWithRequest:request

               completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {

                         if (error == nil) {// 将要处理的内容放在GCDblock中就可以了

                                   dispatch_async(dispatch_get_main_queue(), ^{

                                              // do something with the data

                                   });

                         } else {

                                    NSLog(@"hhhhda");

                          }

               }];

               [dataTask resume];

}

相关文章

网友评论

      本文标题:iOS9.0 NSURLSession 网络请求,出现的错误解决

      本文链接:https://www.haomeiwen.com/subject/fwdclttx.html