UIViewController presentViewController:animated:completion - 启动需要 4 到 6 秒 [英] UIViewController presentViewController:animated:completion - taking 4 to 6 seconds to launch

查看:21
本文介绍了UIViewController presentViewController:animated:completion - 启动需要 4 到 6 秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个登录模块,其中用户输入的凭据在后端系统中得到验证.我使用异步调用来验证凭据,在用户通过身份验证后,我使用 presentViewController:animated:completion 方法进入下一个屏幕.问题是 presentViewController 方法的启动在呈现下一个屏幕之前需要花费一些时间.恐怕我之前对 sendAsynchronousRequest:request queue:queue completionHandler: 的调用会产生副作用.

I'm building a login module where the credentials entered by the user are validated in a backend system. I'm using an asynchronous call to validate credentials and after user has been authenticated I proceed to next screen using the method presentViewController:animated:completion. The problem is that the presentViewController method launching is taking an abornal time until presenting the next screen. I’m afraid that my previous call to the sendAsynchronousRequest:request queue:queue completionHandler: is somehow creating a side effect.

只是为了确保我说 4-6 秒是在命令 presentViewController:animated:completion 启动之后.我这么说是因为我正在调试代码并监控调用方法的时刻.

Just to make sure when I say 4 – 6 seconds is after the command presentViewController:animated:completion is started. I'm saying it because I’m debugging the code and monitoring the moment where the method is called.

首先:调用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 方法被调用,运行异常时间

Second: UIViewController method is called taking an abnormal time running

UIViewController *firstViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstView"];

[self presentViewController:firstViewController animated:YES completion:nil];

感谢任何帮助.

谢谢,马科斯.

推荐答案

这是从后台线程操作 UI 的典型症状.您需要确保只在主线程上调用 UIKit 方法.不能保证在任何特定线程上调用完成处理程序,因此您必须执行以下操作:

This is a classic symptom of manipulating the UI from a background thread. You need to make sure you only call UIKit methods on the main thread. The completion handler isn't guaranteed to be called on any particular thread so you must do something like this:

[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];
    });
}

这可以保证您的代码在主线程上运行.

This guarantees your code runs on the main thread.

这篇关于UIViewController presentViewController:animated:completion - 启动需要 4 到 6 秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆