microsoft / clarity-apps

Repo for distributing Clarity Apps packages
MIT License
43 stars 2 forks source link

Crash on initialization for subclass of CAGradientLayer #44

Open AndriiHanets opened 1 month ago

AndriiHanets commented 1 month ago

SDK version 1.0.0

Example code

// SDK setup in AppDelegate
let clarityConfig = ClarityConfig(projectId: "xxx")
ClaritySDK.initialize(config: clarityConfig)

class CustomGradientLayer: CAGradientLayer {

   init(anyValue: Any?) {
        super.init()
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}

// in view controller
override func viewDidLoad() {
        super.viewDidLoad()

        view.layer.addSublayer(CustomGradientLayer(anyValue: nil))
}

Crash logs: App/CustomGradientLayer.swift:11: Fatal error: Use of unimplemented initializer 'init(layer:)' for class 'App.CustomGradientLayer'

ibradwan commented 1 month ago

Hello @AndriiHanets,

Thanks a lot for reporting this issue. We're currently working on investigating it and hopefully a hotfix will be released soon. Please keep the feedback coming.

ibradwan commented 1 month ago

For now to unblock yourself, you can just add:

override init(layer: Any) {
    super.init(layer: layer)
}

to your custom layer and this should take care of that.

The root cause of the problem is that Clarity depends on presentation layers to capture ongoing animations. The presentation() function internally makes use of the initializer that takes a layer as an input. Now on the Swift side, if you provide one initializer, your class does not inherit the rest of the super class initializers, hence the missing initializer error.