Block circular reference problem in iOS

Source: Internet
Author: User

1, block is a property of the Controller object, the use of self inside the block will cause the loop application

void (^testblock) (); @interface Secondviewcontroller () @property (nonatomic, copy) Testblock Testblock; @end  = ^()  {      NSLog (@ "%@", Self.mapview);  };  Self.testblock ();

2, the block inside out as a self method, when using Weakself call this method, and this method has the property of self, block does not cause memory leaks

Self.testblock = ^() {       [weakself test];}; -(void) test{    NSLog (@ "%@", Self.mapview);}

3. When the block is not a property of self, the use of self within the block does not cause a memory leak

Testblock Testblock = ^() {      NSLog (@ "%@", Self.mapview);}; [Self test:testblock];

4, when using the block as a parameter to use, the block internal use of self also does not cause memory leaks

- (void) viewdidload {[Super viewdidload]; [Self getData:^ (NSString *str) {NSLog (@"%@----%@", self, str);       }]; [ Selfclass] getdata:^ (NSString *str) {NSLog (@"class method--%@---%@", self, str); }];}- (void) GetData: (successblock) success{if(Success) {Success (@"Success"); }}+ (void) GetData: (successblock) success{if(Success) {Success (@"Success"); }}

Block circular reference problem in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.