首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >UIViewController presentViewController:动画:完成-启动需要4到6秒

UIViewController presentViewController:动画:完成-启动需要4到6秒
EN

Stack Overflow用户
提问于 2013-02-26 08:32:16
回答 1查看 5K关注 0票数 2

我正在构建一个登录模块,其中用户输入的凭据在后端系统中进行验证。我使用一个异步调用来验证凭据,在用户通过身份验证后,我使用presentViewController:animated:completion方法进入下一个屏幕。问题是,启动presentViewController方法需要一段时间才能显示下一个屏幕。我担心我之前对sendAsynchronousRequest:request queue:queue completionHandler:的调用在某种程度上产生了副作用。

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

首先:调用NSURLConnection方法:

代码语言:javascript
复制
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方法会占用异常的运行时间

代码语言:javascript
复制
UIViewController *firstViewController = [self.storyboard instantiateViewControllerWithIdentifier:@"FirstView"];

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

任何帮助都是非常感谢的。

谢谢,马科斯。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-02-26 08:40:29

这是从后台线程操作UI的典型症状。您需要确保只在主线程上调用UIKit方法。不能保证在任何特定的线程上都会调用完成处理程序,所以您必须这样做:

代码语言:javascript
复制
[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];
    });
}

这保证了您的代码在主线程上运行。

票数 12
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15079232

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档