From 42755eb263602ddbf1170b88affb560227365bc8 Mon Sep 17 00:00:00 2001 From: Stephen Celis Date: Thu, 15 Aug 2024 09:53:08 -0700 Subject: [PATCH] Delay `UIAlertController.onDismiss` (#201) Currently, `onDismiss` is called _before_ the `UIAlertAction` is processed, which generally isn't an issue, but if you care about the order of operations, this can be a bit thorny. In the case of highly generalized navigation patterns in TCA, the order is what we use to emit warnings when invalid actions are received (like a `dismiss` action is received for an already-dismissed feature), so let's address the problem with a quick tick. We could do this thread hop unconditionally if it makes sense to, but let's localize to `UIAlertController` for now. --- Sources/UIKitNavigationShim/shim.m | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/Sources/UIKitNavigationShim/shim.m b/Sources/UIKitNavigationShim/shim.m index 4ad540c4c..e473be6e5 100644 --- a/Sources/UIKitNavigationShim/shim.m +++ b/Sources/UIKitNavigationShim/shim.m @@ -51,8 +51,15 @@ - (void)UIKitNavigation_viewDidDisappear:(BOOL)animated { [self UIKitNavigation_viewDidDisappear:animated]; if ((self.isBeingDismissed || self.isMovingFromParentViewController) && self.onDismiss != NULL) { - self.onDismiss(); - self.onDismiss = nil; + if ([self isKindOfClass:UIAlertController.class]) { + dispatch_async(dispatch_get_main_queue(), ^{ + self.onDismiss(); + self.onDismiss = nil; + }); + } else { + self.onDismiss(); + self.onDismiss = nil; + } } }