diff --git a/CHANGELOG.md b/CHANGELOG.md index 9bcbcd664..96d91fc49 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,4 +4,5 @@ - Add support for IGListKit post-removal-of-IGListSectionType, in preparation for IGListKit 3.0.0 release. (Adlai-Holler)[https://github.com/Adlai-Holler] (#49)[https://github.com/TextureGroup/Texture/pull/49] - Fix `__has_include` check in ASLog.h [Philipp Smorygo](Philipp.Smorygo@jetbrains.com) - Fix potential deadlock in ASControlNode [Garrett Moon](https://github.com/garrettmoon) -- [Yoga Beta] Improvements to the experimental support for Yoga layout [Scott Goodson](appleguy) \ No newline at end of file +- [Yoga Beta] Improvements to the experimental support for Yoga layout [Scott Goodson](appleguy) +- Fix inserting a sublayer in an index of NSNotFound (Michael Schneider)[https://github.com/maicki] (#80)(https://github.com/TextureGroup/Texture/pull/80) diff --git a/Source/ASDisplayNode.mm b/Source/ASDisplayNode.mm index 303775a5f..e0a7f54e5 100644 --- a/Source/ASDisplayNode.mm +++ b/Source/ASDisplayNode.mm @@ -2767,6 +2767,8 @@ - (void)_insertSubnode:(ASDisplayNode *)subnode atSubnodeIndex:(NSInteger)subnod _subnodes = [[NSMutableArray alloc] init]; } [_subnodes insertObject:subnode atIndex:subnodeIndex]; + + BOOL nodeWasLoaded = [self _locked_isNodeLoaded]; __instanceLock__.unlock(); // This call will apply our .hierarchyState to the new subnode. @@ -2786,6 +2788,22 @@ - (void)_insertSubnode:(ASDisplayNode *)subnode atSubnodeIndex:(NSInteger)subnod } } else if (self.nodeLoaded) { // If not rasterizing, and node is loaded insert the subview/sublayer now. + + if (nodeWasLoaded == NO && sublayerIndex == NSNotFound) { + // In this case the node was loaded while setting the _setSupernode. We have to grab the sublayer index again + __instanceLock__.lock(); + NSInteger idx = [_subnodes indexOfObjectIdenticalTo:subnode]; + if (_layer && idx == 0) { + sublayerIndex = 0; + } else if (_layer) { + ASDisplayNode *positionInRelationTo = (_subnodes.count > 0 && idx > 0) ? _subnodes[idx - 1] : nil; + if (positionInRelationTo) { + sublayerIndex = incrementIfFound([_layer.sublayers indexOfObjectIdenticalTo:positionInRelationTo.layer]); + } + } + __instanceLock__.unlock(); + } + [self _insertSubnodeSubviewOrSublayer:subnode atIndex:sublayerIndex]; } // Otherwise we will insert subview/sublayer when we get loaded