From 10d258b6b76be750c45d224bbbae0b5552ff1416 Mon Sep 17 00:00:00 2001 From: Adlai Holler Date: Wed, 22 Feb 2017 17:48:13 -0800 Subject: [PATCH] Prevent Adding View-Backed Nodes to Layer-Backed Hierarchies (#3062) * Prevent adding view-backed nodes to layer-backed nodes * Do that in a different diff * Fix the message * Update tests * Fix the fix --- AsyncDisplayKit/ASDisplayNode.mm | 7 ++++++- AsyncDisplayKitTests/ASDisplayNodeTests.mm | 14 +++++++------- examples/ASDKgram/Sample/CommentsNode.m | 1 + 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/AsyncDisplayKit/ASDisplayNode.mm b/AsyncDisplayKit/ASDisplayNode.mm index 9e36173275..4f0e88bb60 100644 --- a/AsyncDisplayKit/ASDisplayNode.mm +++ b/AsyncDisplayKit/ASDisplayNode.mm @@ -2543,6 +2543,11 @@ - (void)_insertSubnode:(ASDisplayNode *)subnode atSubnodeIndex:(NSInteger)subnod return; } + if (self.layerBacked && !subnode.layerBacked) { + ASDisplayNodeFailAssert(@"Cannot add a view-backed node as a subnode of a layer-backed node. Supernode: %@, subnode: %@", self, subnode); + return; + } + __instanceLock__.lock(); NSUInteger subnodesCount = _subnodes.count; __instanceLock__.unlock(); @@ -3169,7 +3174,7 @@ - (void)setHierarchyState:(ASHierarchyState)newState ASDisplayNodeAssert(_flags.synchronous == NO, @"Node created using -initWithViewBlock:/-initWithLayerBlock: cannot be added to subtree of node with shouldRasterizeDescendants=YES. Node: %@", self); } - // Entered or exited contents rendering state. + // Entered or exited range managed state. if ((newState & ASHierarchyStateRangeManaged) != (oldState & ASHierarchyStateRangeManaged)) { if (newState & ASHierarchyStateRangeManaged) { [self enterInterfaceState:self.supernode.interfaceState]; diff --git a/AsyncDisplayKitTests/ASDisplayNodeTests.mm b/AsyncDisplayKitTests/ASDisplayNodeTests.mm index aabe79de5d..f4485d2603 100644 --- a/AsyncDisplayKitTests/ASDisplayNodeTests.mm +++ b/AsyncDisplayKitTests/ASDisplayNodeTests.mm @@ -1098,33 +1098,34 @@ - (void)testSubnodes - (void)testReplaceSubnodeNoView { - [self checkReplaceSubnodeWithView:NO layerBacked:NO]; + [self checkReplaceSubnodeLoaded:NO layerBacked:NO]; } - (void)testReplaceSubnodeNoLayer { - [self checkReplaceSubnodeWithView:NO layerBacked:YES]; + [self checkReplaceSubnodeLoaded:NO layerBacked:YES]; } - (void)testReplaceSubnodeView { - [self checkReplaceSubnodeWithView:YES layerBacked:NO]; + [self checkReplaceSubnodeLoaded:YES layerBacked:NO]; } - (void)testReplaceSubnodeLayer { - [self checkReplaceSubnodeWithView:YES layerBacked:YES]; + [self checkReplaceSubnodeLoaded:YES layerBacked:YES]; } -- (void)checkReplaceSubnodeWithView:(BOOL)loaded layerBacked:(BOOL)isLayerBacked +- (void)checkReplaceSubnodeLoaded:(BOOL)loaded layerBacked:(BOOL)isLayerBacked { DeclareNodeNamed(parent); DeclareNodeNamed(a); DeclareNodeNamed(b); DeclareNodeNamed(c); + DeclareNodeNamed(d); - for (ASDisplayNode *n in @[parent, a, b, c]) { + for (ASDisplayNode *n in @[parent, a, b, c, d]) { n.layerBacked = isLayerBacked; } @@ -1136,7 +1137,6 @@ - (void)checkReplaceSubnodeWithView:(BOOL)loaded layerBacked:(BOOL)isLayerBacked [parent layer]; } - DeclareNodeNamed(d); if (loaded) { XCTAssertFalse(d.nodeLoaded, @"Should not yet be loaded"); } diff --git a/examples/ASDKgram/Sample/CommentsNode.m b/examples/ASDKgram/Sample/CommentsNode.m index 67859bd37f..88d2cb78c1 100644 --- a/examples/ASDKgram/Sample/CommentsNode.m +++ b/examples/ASDKgram/Sample/CommentsNode.m @@ -96,6 +96,7 @@ - (void)createCommentLabels for (NSUInteger i = 0; i < numLabelsToAdd; i++) { ASTextNode *commentLabel = [[ASTextNode alloc] init]; + commentLabel.layerBacked = YES; commentLabel.maximumNumberOfLines = 3; [_commentNodes addObject:commentLabel];