我链接了几个dispatch_async
方法,以便它们对系统中的用户进行身份验证。
下一个异步方法仅在前一个方法完成时发生,因为它们每个方法都有完成处理程序。
当最后一个完成后,我执行一个带有 2 个uiview
动画块的自定义 segue。
但是,当我在实际运行时记录时,日志和实际发生的动画之间存在相当大的差距,最终视图会动画化并调用完成块。
我真的不知道在这里添加我的代码会有多大用处,但我已经测试过,它必须是异步方法,因为如果我将它们注释掉并返回YES
动画就会立即发生与日志同时发生。
有谁知道为什么会发生这种情况?
编辑*(带代码)
典型的"存在检查"用于电子邮件、用户、用户 ID。
- (void)existsInSystemWithCompletionHandler:(void (^)(BOOL))block
{
self.existsInSystem = NO;
if (self.isValid) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
//
// Get data
//
if (dataIsValid) {
block(YES);
} else {
block(NO);
}
});
} else {
block(self.existsInSystem);
}
}
检查用户是否存在
[potentialUser existsInSystemWithCompletionHandler:^(BOOL success) {
if (success) {
// Perform segue
//
[self performSegueWithIdentifier:@"Logging In" sender:self];
}
}];
塞格
- (void)perform
{
NSLog(@"Perform");
LogInViewController *sourceViewController = (LogInViewController *)self.sourceViewController;
LoggingInViewController *destinationViewController = (LoggingInViewController *)self.destinationViewController;
destinationViewController.user = sourceViewController.potentialUser;
// Animate
//
[UIView animateWithDuration:0.2f
animations:^{
NSLog(@"Animation 1");
//
// Animate blah blah blah
//
}];
[UIView animateWithDuration:0.4f
delay:0.0f
options:UIViewAnimationOptionCurveEaseIn
animations:^{
NSLog(@"Animation 2");
//
// Animate blah blah blah
//
}
completion:^(BOOL finished) {
NSLog(@"Completion");
//
// Finished
//
[sourceViewController presentViewController:destinationViewController animated:NO completion:nil];
}];
}
在 VC 中登录
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];
[self.user loginWithCompletionHandler:^(BOOL success) {
if (success) {
[self performSegueWithIdentifier:@"Logged In" sender:self];
}
}];
}
固定!现在似乎可以工作,在我的代码中我添加了以下行:
dispatch_async(dispatch_get_main_queue(), ^{
completionBlock(success);
});
从您的描述来看,您的 segue 似乎正在等待一个进程完成。
也许您的嵌套异步方法通过其完成方法相互跟随,产生了一些复杂的代码。这可能是您可能忽略阻止方法的原因。
整理代码的一种方法是使用顺序队列。推送到队列的块只有在前一个块完成后才会启动。