我有以下代码,但不确定为什么它没有运行:
NSURLSession *session = [NSURLSession sharedSession];
NSURLSessionDataTask *dataTask = [session dataTaskWithURL:
[NSURL URLWithString:myURLString] completionHandler:^
(NSData *data, NSURLResponse *response, NSError *error)
{NSDictionary *json =
[NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
[self performSegueWithIdentifier:@"login" sender:self];}];
[dataTask resume];
preformSegueWithIdentifier 在括号外有效,但在括号内无效。但是我使用 NSUrlConnection 做了类似的事情并且工作正常。我是 NSURLSession 的新手,不知道为什么 performSegueWithIdentifier 不在调用中运行。
有什么想法吗?
最佳答案
如果您按原样创建 NSURLSession 对象,它会创建一个新的串行操作队列,而不是主队列。 dataTaskWithURL: 方法的完成处理程序在 session 的队列上运行,因此它不起作用,因为您正试图在后台队列上执行 UI 任务。要修复它,请像这样创建 session ,
NSURLSession *session = [NSURLSession sessionWithConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration] delegate: nil delegateQueue: [NSOperationQueue mainQueue]];
关于ios - NSURLSession performSegueWithIdentifier 未运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24295418/