Search code examples
swiftobjective-cuiviewcontrolleruikitswift-concurrency

Calling UIViewController dismiss and/or present in an async context?


According to Calling Objective-C APIs Asynchronously methods imported from Objective-C that meet certain requirements are "imported as two methods", where one is async instead of having a completion block.

If the method has more than one parameter, and the last parameter’s selector piece is one of the following, Swift imports the method as an asynchronous method ...

  • completion

The Objective-C signature for UIViewController present(_:animated:completion:) is presentViewController:animated:completion:.

So based on this present ought to have an async alternative that looks like:

func present(_ viewControllerToPresent: UIViewController, animated flag: Bool) async

Similarly for other methods, like dismiss.

But Xcode doesn't seem to have any built-in async versions of these methods.

I think these methods are still imported from Objective-C… if you open the definition for present, there's an option to open the "Original Source" and it's Objective-C.

Am I misunderstanding the "Calling Objective-C APIs Asynchronously" document? Or is there some other reason why Swift doesn't import these methods with async alternatives?


Solution

  • They're marked with NS_SWIFT_DISABLE_ASYNC in the header file to explicitly prevent the async versions from being generated

    Credit to Dan in the comments above