Don't let let audio session tokens be double consumed

This commit is contained in:
Shadowfacts 2024-07-09 23:27:18 -07:00
parent 8322d3a36c
commit ce99352e90
1 changed files with 6 additions and 1 deletions

View File

@ -34,6 +34,8 @@ final class AudioSessionCoordinator {
} }
func endPlayback(token: Token, completionHandler: (() -> Void)? = nil) { func endPlayback(token: Token, completionHandler: (() -> Void)? = nil) {
// mark the token as consumed, so when it's deinited we don't try to end again
token.consumed = true
// the enqueued block can't retain token, since it may be being dealloc'd right now // the enqueued block can't retain token, since it may be being dealloc'd right now
let mode = token.mode let mode = token.mode
queue.async { queue.async {
@ -70,13 +72,16 @@ final class AudioSessionCoordinator {
final class Token { final class Token {
let mode: Mode let mode: Mode
fileprivate var consumed = false
init(mode: Mode) { init(mode: Mode) {
self.mode = mode self.mode = mode
} }
deinit { deinit {
AudioSessionCoordinator.shared.endPlayback(token: self) if !consumed {
AudioSessionCoordinator.shared.endPlayback(token: self)
}
} }
} }