Block analysis and using block lead to cyclic strong reference example

Source: Internet
Author: User
Tags variable scope

    1. Definition syntax
    2. External variable Value intercept
    3. Change the value of an external variable
    4. The block variable scope causes the block body to copy from the stack to the heap while the variable holds the variable intercepted in the block body
    5. Block variable scope causes strong references

typedef void (^blocktype) (NSString *str); Declares a block type of void (^) (NSString *str) with the type name Blocktype
-(void) viewdidload
{

  • int i=[self getvaluesfrom:^int (int i, int y) {//algorithm determined by itself, value passed by others
  • return i+y;
  • }];//got the results of his own algorithm.
  • NSLog (@ "i=%d", I);
  • __block int test=100;
  • NSLog (@ "Test is:%d", test);//100
  • void (^BLOCK1) () =^{
  • NSLog (@ "Block1 intercepted test is:%d", test);
  • test=110;
  • };
  • NSLog (@ "Test is:%d", test);//100
  • Test+=1;
  • NSLog (@ "Test is:%d", test);//101
  • void (^block2) () =^{
  • NSLog (@ "Block2 intercepted test is:%d", test);
  • test=120;
  • };
  • NSLog (@ "Test is:%d", test);//101
  • Test+=1;
  • NSLog (@ "Test is:%d", test);//102
  • Block1 (); Executes the BLOCK1, the output test is 102, the value of test is changed to 110
  • NSLog (@ "Test is:%d", test);//110
  • Test+=1;
  • NSLog (@ "Test is:%d", test);//111
  • Block2 ();//execute Block2, output test is 111, change the value of test to 120
  • NSLog (@ "Test is:%d", test);//120
  • Test+=1;
  • NSLog (@ "Test is:%d", test);//121
    • /*
    • It is concluded that block interception of external variable values is not intercepted when the block is implemented, but when the block is executed, the current external variable value is intercepted at execution time.
    • So there is no fear of implementation when the external variable is this value, to the execution of the block when the value of the external variable becomes another, even if more than one block uses the same external variable value, because no matter how many places used, the same moment to manipulate the variable can only have one place, and block is intercepted at execution time. However, it should also be noted that the interception of external variable values is
    • The principle was intercepted at the time of implementation.
    • The assignment of an external variable within a block affects the actual result of the external variable.
    • */
  • void (^testiscanholdself) () =^{
  • NSLog (@ "Self was%@", self);
  • };
  • Testiscanholdself ();
    • /*
    • Does not cause a strong reference, because the scope of the testiscanholdself is the Viewdidload function,
    • When the testiscanholdself variable is discarded, the self-ownership testiscanholdself intercepts will also be released.
    • Declaring Testiscanholdself as a strong member variable of a class causes a circular reference, because
    • Testiscanholdself is a strong reference variable, block body is assigned to testiscanholdself when the block body
    • From the stack to the heap, the block body intercepts self or intercepts only the member variables in self.
    • Testiscanholdself also holds self,
    • Self also holds testiscanhold member variables.
    • If you want to declare the testiscanholdself as the __weak type, then the block body is generated to assign values to
    • __weak's testiscanholdself, no variable holds the block body, then the block body is immediately released again,
    • (The block body can be considered as the object)
    • To resolve the issue of the strong reference, let the testiscanholdself variable not hold self, but block needs to use self
    • So we can get the block body to intercept a __weak self.
    • __weak Viewcontroller *weakself=self;
    • testiscanholdself=^{
    • NSLog (@ "Self is%@", weakself);
    • };
    • Testiscanholdself ();
    • */

}


-(NSString *) testblock{

    • NSString * (^myblock) (nsstring *str) = ^/*nsstring **/(nsstring *str) {
    • return str;
    • };//Inline method
    • return Myblock (@ "testvalues");

}

-(int) Getvaluesfrom:(int (^) (int i,int y)) block{

    • return block (//block), call block in the body

}

Examples of cyclic strong references and solutions:

    • Eocnetworkfetcher holds Completion object NSData object, Nsurl Object
    • Eocclass holds Eocnetworkfetcher object, NSData object,
    • Generating completion stack objects capturing NSData objects in Eocclass
    • Which is the indirect capture of self,
    • The completion stack object is passed into the Eocnetworkfetcher object and is held by its completion object
    • Eocnetworkfetcher will copy the Eocclass completion stack object into the heap.
    • Thus Eocnetworkfetcher objects hold eocclass objects because they hold completion heap objects
    • A cyclic strong reference occurs.

There are two ways to solve this problem:

    • 1. If the holder of the Eocnetworkfetcher object in Eocclass is just its own,
      • Then release hold Eocnetworkfetcher object, Eocnetworkfetcher object has no holder,
      • The memory is then freed, and the member variables inside the Eocnetworkfetcher object are freed, thereby
      • The completion object inside the Eocnetworkfetcher object does not hold the completion in Eocclass, thus
      • The completion object does not have a holder to release memory and will also release the captured Eocclass object
      • Thereby a cyclic strong reference was broken.
    • 2. If the completion object passed into the Eocnetworkfetcher object is Eocclass, only the Eocnetworkfetcher is held,
      • Then let the Eocnetworkfetcher object no longer hold the incoming completion, and thus no longer hold the Eocclass object completion captured
      • Thus the cyclic strong reference is broken, that is, in the appropriate place the completion object in the Eocnetworkfetcher object release to nil can be
      • In general, the second, let the API side to handle the strong reference problem instead of using the API to handle

    • What if the object captured by completion in Eocclass is a __weak variable?
      • If the __weak variable is declared in the method body, then the method body in Eocclass executes, and the __weak variable scope is exceeded to dispose of the object
      • Therefore, the Eocnetworkfetcher object cannot be assigned a value Eocclass object.
      • If the __weak variable is a member variable of the Eocclass object, the variable is not released when it is passed into the Eocnetworkfetcher, Eocnetworkfetcher
      • You can assign a memory value to a Eocclass object __weak member variable as if it solves the problem, but actually completion the captured __weak object
      • A self reference to the Eocclass object is required, and the Eocclass object is captured at this time, so the problem is still not resolved.
    • Or the completion variable in the Eocnetworkfetcher object is __weak?
      • In this way, the completion in the Eocclass is passed in, and the completion in the Eocclass is released when the scope is exceeded.
      • The completion variable in the Eocnetworkfetcher object does not hold the received completion object, so the completion object memory frees the
      • Causes the completion variable in the Eocnetworkfetcher object to always be nil

Block analysis and using block lead to cyclic strong reference example

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.