gpt4 book ai didi

iphone - dismissViewControllerAnimated :completion: has a couple second delay

转载 作者:IT王子 更新时间:2023-10-29 08:18:00 28 4
gpt4 key购买 nike

dismissViewControllerAnimated:completion: 在我的应用程序中工作正常,除了解雇之间的延迟。

[api loginWithUsername:[dict objectForKey:@"username"] andPassword:[dict objectForKey:@"password"] andSuccessBlock:^(id json) {
NSLog(@"DONE... %@", [json objectForKey:@"status"]);
NSString *status = [json objectForKey:@"status"];
if([status isEqualToString:@"ok"]){
app.user = [json objectForKey:@"data"];
[self dismissViewControllerAnimated:YES completion:nil];
}else{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"could not log you in" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil];
[alert show];
}
}];

在我的控制台中,我可以看到回显“DONE...ok”,这意味着回调 block 已执行,但是大约 3-4 秒后模态视图最终被取消。

是什么导致了这种延迟?

最佳答案

如果您不能保证您的 UI 代码在主线程上运行,它可能会在其他线程上运行,在这种情况下,您将遇到几秒钟的延迟!

您可以添加此代码以确保解雇在主线程上运行:

dispatch_async(dispatch_get_main_queue(), ^{
[self dismissViewControllerAnimated:YES completion:nil];
});

一般来说,这不是问题,因为您的大部分代码已经在主线程上运行,因为我们主要添加从 UIKit 方法(如 viewDidLoad 等)运行的代码。这些方法保证在主线程上运行。

当您最终在另一个线程上运行代码时,就会出现问题。可能发生这种情况的一种情况是,例如在网络库的完成 block 调用中,请求在后台完成。

关于iphone - dismissViewControllerAnimated :completion: has a couple second delay,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14341739/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com