0

嗨,我遇到了一种我真的不明白的奇怪行为。

我向用户展示了一个触摸 ID 标识,如果他获得授权,我将调用 [self performSegueWithIdentifier: @"callCustomSegue" sender:self]; 以这种方式在块内:

[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL success, NSError *error) {
                                if (success) {
                                    [self performSegueWithIdentifier: @"callCustomSegue" sender:self];

然后应用程序停止几秒钟(至少 3-4 秒),然后呈现下一个 ViewController。

“callCustomSegue”调用的执行是这样的:

- (void) perform {

    src = (UIViewController *) self.sourceViewController;
    dst = (UIViewController *) self.destinationViewController;

    [src.view addSubview:dst.view];

}

我不明白触摸 ID 上的识别和 performSegueWithIdentifier 之间发生了什么以及应用程序停止的原因。

如果我绕过触摸 ID 并只调用 performSegueWithIdentifier 会立即按我的预期工作。

如果我放入触摸 ID 块:

[myContext evaluatePolicy:LAPolicyDeviceOwnerAuthenticationWithBiometrics
                  localizedReason:myLocalizedReasonString
                            reply:^(BOOL success, NSError *error) {
                                if (success) {
                    authenticated = YES;
                        [self showMessage:@"Authentication is successful" withTitle:@"Success"];
                }

showMessage 在哪里执行此操作:

 UIAlertController * alert=   [UIAlertController
                                  alertControllerWithTitle:title
                                  message:message
                                  preferredStyle:UIAlertControllerStyleAlert];


    UIAlertAction* cancel = [UIAlertAction
                             actionWithTitle:@"OK"
                             style:UIAlertActionStyleDefault
                             handler:^(UIAlertAction * action)
                             {
                                 [alert dismissViewControllerAnimated:YES completion:nil];

                                 if (authenticated) {
                                     [self performSegueWithIdentifier: @"callCustomSegue" sender:self];
                                 }

                                 if (!authenticated) {
                                     [self touchID];
                                 }

                             }];

点击 OK 后,会立即调用下一个 ViewController。

所以问题是:为什么我不能在 touch ID 块中调用 performSegue 并立即得到响应?

知道我哪里错了吗?

太感谢了。

4

1 回答 1

1

您应该在主队列上执行所有与 UI 相关的活动。touchID 进程的reply块不能保证在主队列上执行。事实上,你几乎可以保证它不会。

你应该有 -

  if (success) {
     authenticated = YES;
     dispatch_async(dispatch_get_main_queue(), ^{ 
        [self performSegueWithIdentifier: @"callCustomSegue" sender:self];
     });
于 2014-10-30T10:07:05.803 回答