search

Home  >  Q&A  >  body text

objective-c - Question about why Masonry does not cause circular references

The code is an example. I don’t know if my understanding is correct.

[self.view mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.otherView.mas_centerY);
}];

The block holds self, but self.view does not hold this block, because the source code of Masonry is as follows:

- (NSArray *)mas_makeConstraints:(void(^)(MASConstraintMaker *))block {
    self.translatesAutoresizingMaskIntoConstraints = NO;
    MASConstraintMaker *constraintMaker = [[MASConstraintMaker alloc] initWithView:self];
    block(constraintMaker);
    return [constraintMaker install];
}

It is just block(constrainMaker). If it is changed to self.block = block(constrainMaker), does the view also hold the block?

高洛峰高洛峰2836 days ago691

reply all(1)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-02 09:32:40

    It is not true that block will definitely cause a circular reference. Whether it is a circular reference depends on whether they hold strong references to each other. If self is used in a block, the block will keep a reference to self, but self does not hold the block directly or indirectly, so it will not cause a circular reference.

    Your understanding is correct.

    reply
    0
  • Cancelreply