Async/await and modal dialogs

In a UIKit context, has anyone had experience/success in using async/await to synchronize a modal dialog with other logic? I've tried it a bit without success.

I.e, given a presented dialog, I want to capture data in the dialog, then use the results in a simple, linear fashion. (Something that looks like "Present the dialog, wait for results, use results" -- all inline without closures.)

It seems to me that async/await with @MainActor ought to make that possible, but I haven't yet figured out how. I'd really like to see a real-world example.

Can you help?

What's the specific dialog API you want to use (the non-asynchronous one that you would have used before, I mean)?

I'm thinking about two scenarios:

(1) I want to present a dialog (let's say, a data collection form). So I write a function:
func myForm(_ inData: MyDataStruct, completion: (()->MyDataStruct)?)

which instantiates a view controller, presents it, and calls the completion handler in response to Save or Cancel buttons. This can presumably be converted to async/await using withCheckedContinuation.

If I understand things correctly, this would result in a function:
func myForms(_ inData: MyDataStruct) async -> MyDataStruct

(BUT [Q]: [or at least a subject for my own experimentation] what happens if the dialog is dismissed with a swipe (i.e. by some user action other than tapping one of the "expected" buttons?) — how does continuation.resume get called in this case?)

(2) I want to present an alert (using UIAlertController) and wait for it to return before continuing. Do I handle this the same way? (I.e., wrap it in a call to withCheckedContinuation)?

This is my first foray into async/await, and I'd appreciate a bit of support (even if just in the form of "yes, that's the way to go") before I spend time getting lost in the weeds.

Thanks.

You probably want to return an optional value, with nil being an indication of dismissal.

But you need to find a reliable way of getting notified about that from UIKit. Otherwise pending continuation will leak task-related resources.

That wasn't the non-asynchronous API I was asking about. I meant to ask about what UIKit API you're using.

Incidentally, the optional completion parameter seems unlikely. For an async wrapper of a function like this, there has to be a completion handler. Even for non-async code, you can't do much with a dialog result if there's no handler to deal with it.

Yep, that sounds like the right thing to do, although I'd push it down into wrapping the UIKit code, not your code.

Again, it depends on what UIKit API you want to use. It should have a completion handler of its own (always called no matter how the interaction ends).