I ran into the strangest of problems: sometimes adding a sublayer to a CALayer does not work ([layer addSublayer:child]), i.e. the sublayer count remains at 0 and nothing is drawn to the screen.
Sometimes it does work and the child layer shows up on the screen as I would have expected. When it does not, usually performing a clean build fixes it, but not always. I also experimented with running two instances of my app from the Terminal, usually the second instance shows the problematic behavior.
Here is the (abbreviated) code:
NSLog(@"Retain count before: %d", [childLayer retainCount]);
// [parentLayer insertSublayer:childLayer atIndex:0]; // same problem here
[parentLayer childLayer];
NSLog(@"Retain count after: %d layer: %@", [childLayer retainCount], childLayer);
NSLog(@"Sublayercount: %d", [[parentLayer sublayers] count]);
The child layer is a basic layer with the background set to a RGB color.
Debug output:
layer: <CALayer: 0x1982b0> X: 50.00 Y: -90.00 width: 200.00 height: 200.00
Retain count before: 2
Retain count after: 2 layer: <CALayer: 0x1982b0>
Sublayercount: 0
When it does work, the above output shows the retain count being increased correctly to 3 and the sublayercount to 1.
Is there something special about CALayers? Does addSublayer rely on something else/perform validity checks of the sublayer?
Can anybody suggest a way to debug this? Is there a way how I can step into addSublayer with the debugger?
Thanks! Mark.