Is it safe to put AVAudioSession calls not on main thread?

We have noticed that sometimes our app spends too much time in the first call of AVAudioSession.sharedInstance and [AVAudioSession setCategory:error:] which we call on app's initialization (during init of apps delegate). I am not sure if the app is stuck in these calls or it simply takes too much time to complete.

This probably causes the app to crash due to main thread watchdog.

Would it be safe to move these calls to a separate thread?

Answered by DTS Engineer in 700304022

It's unlikely that calling setCategory:error: is itself going to trigger the watchdog, though it's possible that if you're doing a lot of other work at initialization time this might send your usage "over the top".

Anyway, app delegate initialization seems way to early to set up your audio session. You should wait at least until the "didFinishLaunching" callback, or — better still, as recommended by the note in the documentation — wait until you first need to play or record some audio, so that you don't interrupt other app's audio until/unless you need to.

The documentation recommends this be done only at the time of playback or record but yet it shows an example using the app delegate.

https://developer.apple.com/documentation/avfaudio/avaudiosession

Accepted Answer

It's unlikely that calling setCategory:error: is itself going to trigger the watchdog, though it's possible that if you're doing a lot of other work at initialization time this might send your usage "over the top".

Anyway, app delegate initialization seems way to early to set up your audio session. You should wait at least until the "didFinishLaunching" callback, or — better still, as recommended by the note in the documentation — wait until you first need to play or record some audio, so that you don't interrupt other app's audio until/unless you need to.

Is it safe to put AVAudioSession calls not on main thread?
 
 
Q