Introduction to the iOS development block and the circular reference problem for block

Source: Internet
Author: User
Tags gcd

The most important thing to remember about 1:block's circular reference problem is two points:

If "block internal" accesses "object a" using "strongly referenced externally declared", then "block internal" automatically produces a "strong reference" to "object A"

If "block internal" accesses "object a" using "weakly referenced externally", then "block internal" automatically produces a "weak reference" pointing to "object a"

2:

#import "ViewController.h"#import "XMGPerson.h"@interfaceViewcontroller () @property (nonatomic, copy)int(^sumblock) (int,int); @property (nonatomic, assign)intA;@end@implementationViewcontroller- (void) viewdidload {[Super viewdidload];    [Self test5]; /*if "block internal" accesses "object a" using "strongly referenced externally declared", then "block internal" automatically produces a "strong reference" to "Object A" if "block internal" uses "external declared weak reference" to access "object A", then "block internal" will be self- To create a "weak reference" pointing to "object a"*/}/** * NOT OK*/- (void) test6{Xmgperson*p =[[Xmgperson alloc] init]; __weak Xmgperson*WEAKP =p; P.name=@"Jack"; P.block= ^{//Block1Xmgperson *STRONGP =WEAKP; NSLog (@"Block1--%@", Strongp.name); dispatch_time_t when= Dispatch_time (Dispatch_time_now, (int64_t) (3.0*nsec_per_sec)); Dispatch_after (when, Dispatch_get_main_queue (),^{//Block2NSLog (@"Block2--%@", Weakp.name);    });        }; P.block ();}/** * Feasible*/- (void) test5{Xmgperson*p =[[Xmgperson alloc] init]; __weak Xmgperson*WEAKP =p; P.name=@"Jack"; P.block= ^{//Block1NSLog (@"beign-------"); Xmgperson*STRONGP =WEAKP; dispatch_time_t when= Dispatch_time (Dispatch_time_now, (int64_t) (3.0*nsec_per_sec)); Dispatch_after (when, Dispatch_get_main_queue (),^{//Block2//NSLog (@ "Block2-%@", weakp.name);NSLog (@"Block2--%@", Strongp.name); Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (2.0* nsec_per_sec)), Dispatch_get_main_queue (), ^{//Block3NSLog (@"Block3--%@", Strongp.name);                    });    });        }; P.block ();}/** * NOT OK*/- (void) test4{Xmgperson*p =[[Xmgperson alloc] init]; P.name=@"Jack"; P.block= ^{//Block1NSLog (@"beign-------"); dispatch_time_t when= Dispatch_time (Dispatch_time_now, (int64_t) (3.0*nsec_per_sec)); Dispatch_after (when, Dispatch_get_main_queue (),^{//Block2NSLog (@"After -----------%@", p.name);    });        }; P.block ();}/** * NOT OK*/- (void) test3{Xmgperson*p =[[Xmgperson alloc] init]; __weak Xmgperson*WEAKP =p; P.name=@"Jack"; P.block= ^{//Block1NSLog (@"beign-------"); Xmgperson*STRONGP =WEAKP; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0*nsec_per_sec)), Dispatch_get_main_queue (),^{//Block2NSLog (@"After -----------%@", Strongp.name);    });        }; P.block ();}/** * Feasible*/- (void) test2{Xmgperson*p =[[Xmgperson alloc] init]; __weak Xmgperson*WEAKP =p; P.name=@"Jack"; P.block= ^{//Block1NSLog (@"beign-------"); Xmgperson*STRONGP =WEAKP; Dispatch_after (Dispatch_time (Dispatch_time_now, (int64_t) (3.0*nsec_per_sec)), Dispatch_get_main_queue (),^{//Block2NSLog (@"After -----------%@", Strongp.name);    });        }; P.block ();}- (void) test1{Xmgperson*p =[[Xmgperson alloc] init]; __weak Xmgperson*WEAKP =p; P.name=@"Jack"; P.block= ^{NSLog (@"-----------%@", Weakp.name); };}#pragmaMark-Other-(void) Test: (int(^) (int,int) ) sumblock{}- (void) Run: (int) a{}- (void) testblock{[self test:^(intAintb) {        returnA +b;            }]; void(^block) () =  ^{NSLog (@"-------");    };                    Block (); int(^sumblock) (int,int) = ^(intNUM1,intnum2) {        returnNUM1 +num2;    }; Sumblock (Ten,Ten); intA =Ten; /*return value type (^block variable name) (parameter type list) = ^ (parameter list) {//block code};     The variable name of the block (argument list); */}@end

Summary: The definition of 1:block: 1:block is defined by the property, with copy decoration,

@property (nonatomic, copy) void (^block) (), no parameter no return value, because block is in the heap by default, you need to block,copy in the heap to the stack to use 2:block's non-attribute definitions: 1: No parameter no return value:

where void (^block) () indicates that a type that declares a block,block named block is represented as: void (^) (), the right block for the block task,
    Block (); indicates that block is called and the left block on the right is assigned a value
  void (^block) () =  ^{        NSLog (@ "-------");    };    
2: A block with parameters with a return value: The left Int (^sumblock) (int, int) defines a block:sumblock with a parameter with a return value, and the right block assigns a value to the left block. notation: The parameters are written in ^ (int a,int b) where AB is the parameter of the block, and the return value is written in block code blocks.
Sumblock (10, 10); Call block, no block pass parameter
^ (int num1, int num2) {        return num1 + num2;    };
  Int (^sumblock) (int, int) = ^ (int num1, int num2) {        return num1 + num2;    };    Sumblock (ten);      
3:block is passed as a parameter: 1:block is passed as a parameter-(void) test: (int (^) (int, int)) Sumblock, implements the method, calls block passes parameter 2: Calls the test method externally, where AB is the passed parameter,

[Self test:^ (int a, int b) {

NSLog (@ "%d", a+b);

return a + B;

}];

-(void) test: (int (^) (int, int)) Sumblock

{

Sumblock (2,1);

}

4:1:block Circular Reference: Xmgperson alloc after init generates an object in memory, name and block are properties of that object (because it is also equivalent to a strong reference with copy decoration), then the object has a strong reference to both properties. After the object is created, there is a strong pointer p pointing to the object (the pointer holds the address of the object, whether the variable is a global variable or a local variable will have a strong reference), __weak that part of the code is to copy the address of the strong pointer p to a weak pointer weakself, when the weak pointer Weakself will have a weak reference to the person object, p.name = @ "Jack"; at this point the Name property will have a strong reference to JACK, and block will have a strong reference to the assigned block. The task in block code blocks is not immediately called, but it detects that there are no references to external variables in the block code blocks (even if the external unreferenced, referenced in after, or an external variable referenced in block, which has a strong or weak reference to the object being accessed). The code block has a reference WEAKP, so a weak reference is generated, and then the code calls block, executes the task in block code blocks, assigns a weak reference WEAKP pointer to a strong reference, and the STRONGP in the block code blocks has a strong reference to the person object , in the execution of the GCD delay function, the block block that generates a delay function, detects a variable in the delay function that has no reference to the outside, and a reference to STRONGP, produces a strong reference to the person object, at which point the code block does not execute immediately (but the system will have a strong pointer to that code block, 2), this time will be executed to the end of the method, strong pointer p destroyed, the weak pointer weakp destroyed, STRONGP execution finished destroying, but at this point Block2 also point to the person object, so the person object will not be destroyed, can print out the name  2: Like a delay function similar to GCD in block, want to keep the object to be unprepared to destroy, just define a strong reference in the block to point to it.








Introduction to the iOS development block and the circular reference problem for block

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.