Ios UIViewController presentViewController:动画:完成-启动需要4到6秒
我正在构建一个登录模块,用户输入的凭证将在后端系统中进行验证。我正在使用一个异步调用来验证凭据,在用户经过身份验证后,我使用方法Ios UIViewController presentViewController:动画:完成-启动需要4到6秒,ios,objective-c,uiviewcontroller,Ios,Objective C,Uiviewcontroller,我正在构建一个登录模块,用户输入的凭证将在后端系统中进行验证。我正在使用一个异步调用来验证凭据,在用户经过身份验证后,我使用方法presentViewController:animated:completion进入下一个屏幕。问题是presentViewController方法启动需要一段异常时间才能显示下一个屏幕。我之前对sendAsynchronousRequest:request queue:queue completionHandler:的调用可能会产生副作用 只是为了确保我说的4–6秒
presentViewController:animated:completion
进入下一个屏幕。问题是presentViewController
方法启动需要一段异常时间才能显示下一个屏幕。我之前对sendAsynchronousRequest:request queue:queue completionHandler:
的调用可能会产生副作用
只是为了确保我说的4–6秒是在命令presentViewController:animated:completion
启动之后。我这么说是因为我正在调试代码并监视调用方法的时刻
第一:调用NSURLConnection
方法:
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:10.0];
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error)
第二:UIViewController
方法被调用以获取异常运行时间
UIViewController *firstViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstView"];
[self presentViewController:firstViewController animated:YES completion:nil];
感谢您的帮助
谢谢,
Marcos.这是从后台线程操作UI的典型症状。您需要确保只在主线程上调用
UIKit
方法。不能保证在任何特定线程上调用完成处理程序,因此必须执行以下操作:
[NSURLConnection sendAsynchronousRequest:request queue:queue completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
dispatch_async(dispatch_get_main_queue(), ^{
UIViewController *firstViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstView"];
[self presentViewController:firstViewController animated:YES completion:nil];
});
}
这保证了代码在主线程上运行。那么您是从完成块中调用表示代码吗?是的,我是从完成块中调用它