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

Copy yogaChildren in accessor method. Avoid using accessor method internally #1283

Merged
merged 2 commits into from
Dec 20, 2018

Conversation

maicki
Copy link
Contributor

@maicki maicki commented Dec 13, 2018

No description provided.

@@ -58,7 +58,7 @@ - (void)setYogaChildren:(NSArray *)yogaChildren

- (NSArray *)yogaChildren
{
return _yogaChildren;
return [_yogaChildren copy] ?: @[];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already lock on the setter, why not go ahead and lock here and declare the property atomic?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this diff was copied from an 11th-hour internal hotfix! But you're right, we should do it now. @maicki mind making the change and landing it with this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew I'd been here before...

@@ -189,7 +189,7 @@ AS_EXTERN void ASDisplayNodePerformBlockOnEveryYogaChild(ASDisplayNode * _Nullab
@interface ASDisplayNode (Yoga)

// TODO: Make this and yogaCalculatedLayout atomic (lock).
@property (nullable, nonatomic) NSArray *yogaChildren;
@property (nonatomic, copy) NSArray *yogaChildren;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't really copy

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think there is a keyword to specify copy-on-read, this one is supposed to mean copy-on-set

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's both

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You mean the keyword means both, or this class is somehow both? Oh the second, yes, I see we do basically copy after all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the official doc, it's pretty unclear because they assume your ivar is immutable. https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

If your ivar is mutable and the property is atomic, there's no other choice than copying on read. Our property isn't atomic, but it should be and we badly assume that it is.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The official doc doesn't seem unclear at all: "Copy Properties Maintain Their Own Copies"

But in our case, we are copying in our setter too (which I missed at first), so I agree copy is a correct attribute.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs are unclear unless you read them to mean that we are not allowed to use a mutable ivar i.e. we have to "maintain our own copy" as an immutable variable at all times. If so, then screw the docs.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation happens to use immutable copies in the examples, but I don't see anything stating or implying that's required (a mutable backing var, as we can see, becomes more complicated, which is why it wouldn't have been great for example code anyway). So no screwing required!

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it's assumed by the example code, there's no guidance on the semantics of getters that are backed by mutable ivars. That's the problem with using it as an example!

@maicki
Copy link
Contributor Author

maicki commented Dec 18, 2018

@Adlai-Holler @wiseoldduck I don't remember where we land on, but could you give that another look please, I updated the yogaChildren getter to make the property atomic.

Copy link
Member

@wiseoldduck wiseoldduck left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@maicki maicki merged commit ab0a00c into master Dec 20, 2018
@maicki maicki deleted the MSCopyYogaChildren branch January 10, 2019 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants