WWDC23 SALE: Save 50% on all my Swift books and bundles! >>

SOLVED: Using async await with older asynchronous Swift API's

Forums > Swift

I am currently in the process of converting my app to use aync await, however I have hit a blocker when it comes to using older asynchronous Swift API methods, more specifically the requestRecordPermission on AVAudioSession.

The ideal would be doing something like this:

@MainActor func startCall(sip: SIP, meeting: Meeting) async throws {
    let granted = try await AVAudioSession.sharedInstance().requestRecordPermission()

    if granted {
        // more stuff here...
        let call = try await callAgent?.startCall(participants: [pstnCallee], options: startCallOptions)
        self.call = call
        self.call?.delegate = self
    } else {
        throw SomeError.custom
    }
}

So I have tried to use the old API using withCheckedThrowingContinuation.

func startCall(sip: SIP, meeting: Meeting) async throws {
    return try await withCheckedThrowingContinuation { continutation -> Void in
        AVAudioSession.sharedInstance().requestRecordPermission { [weak self] granted in
            guard let strongSelf = self else {
                continutation.resume(throwing: SomeError.custom)
            }

            if granted {
                // more stuff here

                do {
                    let call = try await strongSelf.callAgent?.startCall(participants: [pstnCallee], options: startCallOptions)
                    strongSelf.call = call
                    strongSelf.call?.delegate = strongSelf
                    continutation.resume(returning: ())
                } catch {
                    continutation.resume(throwing: error)
                }
            }
        }
    }
}

But Xcode is throwing the following error on line 3: Cannot pass function of type '(Bool) async -> Void' to parameter expecting synchronous function type

Any suggestions on how to fix this?

1      

I have actually figured it out by extending AVAudioSession.

import AVFoundation

extension AVAudioSession {
    func requestRecordPermission() async -> Bool {
        await withCheckedContinuation { continuation in
            requestRecordPermission { granted in
                continuation.resume(returning: granted)
            }
        }
    }
}

Then at the call site:

func requestMicrophonePermission() async -> Bool {
    await AVAudioSession.sharedInstance().requestRecordPermission()
}

1      

Save 50% in my WWDC23 sale.

SAVE 50% To celebrate WWDC23, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.