Why is layer.presentationLayer() returning nil?

580 Views Asked by At

Xcode description says:

Returns nil if the layer has not yet * been committed.

What does this mean in literal sense?

My code is like this:

guard animalView?.layer.presentationLayer() != nil else {
    return QL4("Animal presentation layer was nil \(animalView?.layer)")
}

QL4 prints logs so some of the logs we've received from our users are below. I can't seem to reproduce this and it only happens occasionally. So my question is what is the real reason for presentationLayer() returning nil? My best guess is the device doesn't have enough available power to do this task right away so it does it delays it a little bit?

Animal presentation layer was nil Optional(; animations = { position=; position-2=; }; layer = >; sublayers = (, <_UILabelLayer: 0x172147d0>); allowsGroupOpacity = YES; ; animations = [position= position-2=]>)

Animal presentation layer was nil Optional(; animations = { position=; position-2=; }; layer = >; sublayers = (, <_UILabelLayer: 0x174289ce0>); allowsGroupOpacity = YES; ; animations = [position= position-2=]>)

Animal presentation layer was nil Optional(; animations = { position=; position-2=; }; layer = >; sublayers = (, <_UILabelLayer: 0x170292d40>); allowsGroupOpacity = YES; ; animations = [position= position-2=]>)

Full Code:

animalVC = AnimalVC()
VCOrganizer.mainVCInstance.addAsChildViewController(animalVC!, toView: VCOrganizer.mainVCInstance.view)
animalView = createAnimalView(animalItem!, x: 0, y: 0, sideSize: sideSize)

class AnimalVC: UIViewController {
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        let touch = touches.first
        touchLocation = touch!.locationInView(self.view)
        guard animalView != nil else {
            return QL4("Animal was nil \(animalView)")
        }
        guard animalView?.layer.presentationLayer() != nil else {
            return QL4("Animal presentation layer was nil \(animalView?.layer)")
        }
        if animalView!.layer.presentationLayer()!.hitTest(touchLocation) != nil {
            //                QL1("Clicked animal")
            clickedAnimal()
        } else {
            //                QL1("Clicked on background")
        }
    }
}
0

There are 0 best solutions below