If I ever set an AVPlayerLayer, then there will be some retain cycle that will prevent deinit
from ever being called.
import AVFoundation
class MyPlayer: AVPlayer {
fileprivate(set) lazy var playerLayer: AVPlayerLayer = {
// Create a player layer
$0.videoGravity = AVLayerVideoGravityResizeAspectFill
$0.backgroundColor = UIColor.black.cgColor
return $0
}(AVPlayerLayer(player: self))
override init() {
super.init()
print("MyPlayer init")
_ = playerLayer
}
deinit {
print("MyPlayer deinit")
}
}
Testing with this, only "MyPlayer init" will be printed:
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
_ = MyPlayer()
return true
}