Why -[NSView setLayer: nil] is called by AppKit?

I have a layer backed view with sublayers. It is a part of a larger view. This lager view is removed and added back to some view hierarchy.


When I remove this larger view from its superview, the layer backed view loses the backing layer and therefore all sublayers. So when I add all this back, it doesn't look like it should (layers are missing).


I found that -[NSView setLayer: nil] is called by AppKit from -[NSView removeFromSuperview]:


  * frame #0: 0x00000001228b4ba4 ***`***::setLayer(self=0x0000000104aa1830, cmd="setLayer:", layer=0x0000000000000000) at ****
    frame #1: 0x00007fff84506244 AppKit`-[NSView _setWindow:] + 755
    frame #2: 0x00007fff86a31a52 CoreFoundation`__53-[__NSArrayM enumerateObjectsWithOptions:usingBlock:]_block_invoke + 114
    frame #3: 0x00007fff86a318e6 CoreFoundation`-[__NSArrayM enumerateObjectsWithOptions:usingBlock:] + 198
    frame #4: 0x00007fff84da12a7 AppKit`__21-[NSView _setWindow:]_block_invoke.643 + 151
    frame #5: 0x00007fff84506ae6 AppKit`-[NSView _setWindow:] + 2965
    frame #6: 0x00007fff84509587 AppKit`-[NSView removeFromSuperview] + 465


Any idea why would AppKit do this? Is there a way to avoid this?


I can override -[NSView setLayer:] and do not assign nil layer there but it doesn't look like a good solution.

Why -[NSView setLayer: nil] is called by AppKit?
 
 
Q