I'm getting a crash similar to PaulSarmale
Fatal Exception: NSInvalidArgumentException
0 CoreFoundation 0x9e88 __exceptionPreprocess
1 libobjc.A.dylib 0x178d8 objc_exception_throw
2 CoreFoundation 0x1af0a8 -[__NSCFString characterAtIndex:].cold.1
3 CoreFoundation 0x1abad0 -[__NSArrayM insertObject:atIndex:].cold.2
4 CoreFoundation 0x13f90 -[__NSArrayM insertObject:atIndex:]
5 AVFCore 0x17e58 __22-[AVPlayer _addLayer:]_block_invoke
6 libdispatch.dylib 0x3fdc _dispatch_client_callout
7 libdispatch.dylib 0x13574 _dispatch_lane_barrier_sync_invoke_and_complete
8 AVFCore 0x17548 -[AVPlayer _addLayer:]
9 AVFCore 0x17400 -[AVPlayerLayer _setPlayer:forPIP:]
10 Foundation 0x3f814 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
11 Foundation 0x3f590 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
12 Foundation 0x3eb24 _NSSetObjectValueAndNotify
13 AVFCore 0x176bc -[AVPlayer _addLayer:]
14 AVFCore 0x17400 -[AVPlayerLayer _setPlayer:forPIP:]
15 Foundation 0x3f814 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
16 Foundation 0x3f590 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
17 Foundation 0x3eb24 _NSSetObjectValueAndNotify
18 AVKit 0x21724 -[__AVPlayerLayerView setPlayerController:]
19 AVKit 0x1fdac -[AVPlayerViewController setPlayerController:]
20 Foundation 0x3f814 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
21 Foundation 0x3f590 -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
22 Foundation 0x3eb24 _NSSetObjectValueAndNotify
23 AVKit 0x307f4 -[AVPlayerViewController setPlayer:]
24 <APPNAME> 0x7b8418 closure #1 in AVPlayerBackgroundAudio.setupBecomeActiveSubscription() (<compiler-generated>)
25 Combine 0xb754 Subscribers.Sink.receive(_:)
26 Combine 0xb0a8 protocol witness for Subscriber.receive(_:) in conformance Subscribers.Sink<A, B>
27 Combine 0x12904 closure #1 in Publishers.ReceiveOn.Inner.receive(_:)
28 libswiftDispatch.dylib 0xf58 thunk for @escaping @callee_guaranteed () -> ()
29 libdispatch.dylib 0x24b4 _dispatch_call_block_and_release
30 libdispatch.dylib 0x3fdc _dispatch_client_callout
31 libdispatch.dylib 0x127f4 _dispatch_main_queue_drain
32 libdispatch.dylib 0x12444 _dispatch_main_queue_callback_4CF
33 CoreFoundation 0x9a6f8 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__
34 CoreFoundation 0x7c058 __CFRunLoopRun
35 CoreFoundation 0x80ed4 CFRunLoopRunSpecific
36 GraphicsServices 0x1368 GSEventRunModal
37 UIKitCore 0x3a23d0 -[UIApplication _run]
38 UIKitCore 0x3a2034 UIApplicationMain
39 <APPNAME> 0x7f74 main + 32 (AppDelegate.swift:32)
)
My code is trying to re-attach the player when coming back from the background:
NotificationCenter.default.publisher(for: UIApplication.didBecomeActiveNotification)
.receive(on: DispatchQueue.main)
.sink { [weak self] _ in
guard let self else { return }
if self.playerController?.player == nil && self.player != nil {
self.playerController?.player = self.player
}
}
.store(in: &cancellables)
I even check that the player is not nil directly before the call. Also, it should be valid to assign it to nil anyways.