Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only call -layout and -layoutDidFinish if the node is already loaded #285

Merged
merged 4 commits into from May 19, 2017
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -24,4 +24,5 @@
- [Layout] Extract layout implementation code into it's own subcategories [Michael Schneider] (https://github.com/maicki)[#272](https://github.com/TextureGroup/Texture/pull/272)
- [Fix] Fix a potential crash when cell nodes that need layout are deleted during the same runloop. [Adlai Holler](https://github.com/Adlai-Holler) [#279](https://github.com/TextureGroup/Texture/pull/279)
- [Batch fetching] Add ASBatchFetchingDelegate that takes scroll velocity and remaining time into account [Huy Nguyen](https://github.com/nguyenhuy) [#281](https://github.com/TextureGroup/Texture/pull/281)
- [Layout] Ensure -layout and -layoutDidFinish are called only if a node is loaded. [Huy Nguyen](https://github.com/nguyenhuy) [#285](https://github.com/TextureGroup/Texture/pull/285)

1 change: 0 additions & 1 deletion Source/ASDisplayNode+Layout.mm
Expand Up @@ -293,7 +293,6 @@ - (void)displayNodeDidInvalidateSizeNewSize:(CGSize)size
}
}

/// Needs to be called with lock held
- (void)_locked_measureNodeWithBoundsIfNecessary:(CGRect)bounds
{
// Check if we are a subnode in a layout transition.
Expand Down
18 changes: 13 additions & 5 deletions Source/ASDisplayNode.mm
Expand Up @@ -902,8 +902,10 @@ - (void)__layout
ASDisplayNodeAssertThreadAffinity(self);
ASDisplayNodeAssertLockUnownedByCurrentThread(__instanceLock__);

BOOL loaded = NO;
{
ASDN::MutexLocker l(__instanceLock__);
loaded = [self _locked_isNodeLoaded];
CGRect bounds = _threadSafeBounds;

if (CGRectEqualToRect(bounds, CGRectZero)) {
Expand All @@ -930,17 +932,21 @@ - (void)__layout

[self _layoutSublayouts];

ASPerformBlockOnMainThread(^{
[self layout];
[self layoutDidFinish];
});
// Per API contract, `-layout` and `-layoutDidFinish` are called only if the node is loaded.
if (loaded) {
ASPerformBlockOnMainThread(^{
[self layout];
[self layoutDidFinish];
});
}
}

- (void)layoutDidFinish
{
// Hook for subclasses
ASDisplayNodeAssertMainThread();
ASDisplayNodeAssertLockUnownedByCurrentThread(__instanceLock__);
ASDisplayNodeAssertTrue(self.isNodeLoaded);
}

#pragma mark Calculation
Expand Down Expand Up @@ -1082,8 +1088,10 @@ - (ASLayoutSpec *)layoutSpecThatFits:(ASSizeRange)constrainedSize

- (void)layout
{
// Hook for subclasses
ASDisplayNodeAssertMainThread();
// Subclass hook
ASDisplayNodeAssertLockUnownedByCurrentThread(__instanceLock__);
ASDisplayNodeAssertTrue(self.isNodeLoaded);
}

#pragma mark Layout Transition
Expand Down
4 changes: 3 additions & 1 deletion Source/Private/ASDisplayNodeInternal.h
Expand Up @@ -234,7 +234,9 @@ FOUNDATION_EXPORT NSString * const ASRenderingEngineDidDisplayNodesScheduledBefo
- (void)__setNeedsDisplay;

/**
* Called from [CALayer layoutSublayers:]. Executes the layout pass for the node
* Called whenever the node needs to layout its subnodes and, if it's already loaded, its subviews. Executes the layout pass for the node
*
* This method is thread-safe but asserts thread affinity.
*/
- (void)__layout;

Expand Down