iOS development--block solutions that cause circular references

Source: Internet
Author: User

Memory issues are always a top priority in software development, and iOS development is no exception, and is a must-ask question during an interview. Today we mainly talk about the circular reference problems involved in block. When we start writing code ourselves, we may use self in a lot of blocks, but when we see other people's good code, we find that others often do not use weakself. Why is it? The sample code for this article is uploaded to Https://github.com/chenyufeng1991/Block_WeakSelf.

First of all, let me say the principle of memory management:

1. Strong is used by default and optional weak. Strong a member variable or property, each time a pointer is used to point to an object, retain is automatically called, and release is called on the old object and is set to nil when it needs to be freed.

2. Avoid circular references, otherwise set nil release manually.

3. It is generally necessary to weak the self before creating the block anonymous function, otherwise the loop reference cannot release the controller.

First, Xcode provides us with a good compilation environment, and Xcode warns us if there is a possibility of a circular reference in the code: "Capturing ' self ' strongly in the" is likely to leads to a retain Cycle ".



A circular reference in a block is this: an object has a copy or strong member variable or property, and the member variable or self is directly referenced within the block, which results in the self owning the block member, and the block member holding self, will cause a circular reference. Because self itself is a variable of type strong. Apple's official recommendation is to convert self to weak automatic before passing into the block, so that there is no strong reference to self in the block. If self is freed before block execution is complete, weakself is also set to nil. The weak type is relatively safe because it can be automatically set to nil after release without causing wild pointers. So how to declare it?

1.

__weak typeof (self) weakself = self;

The meaning of this sentence is to declare a self-type weak pointer, named Weakself. typeof is used to find the type of argument, and here is the type of self. This defines the weakself as a type with self and a weak reference to the original self.


2.

__weak typeof (&*self) weakself = self;

3.

__weak Myviewcontroller *weakself = self;


Let me show you through the code:

(1) Declare several blocks and a property:

@interface Viewcontroller () {    void (^myblock1) (void);//The block parameter is void and the return value is void    void (^myblock2) (void);    void (^MYBLOCK3) (void);} @property (nonatomic,copy) nsstring *person; @end

(2) Using weakself does not cause circular references:

    __weak typeof (self) weakself = self;    NSLog (@ "init--> value:%@,address=%p,self=%p", self.person,self.person,self);    MyBlock1 = ^ (void) {        //This does not cause        a circular reference NSLog (@ "execute1--> value:%@,address=%p,weakself=%p", Weakself.person, weakself.person,weakself);    };


(3) Direct use of self, will be circular reference: Xcode will give a warning

    MyBlock2 = ^ (void) {        //This causes a circular reference        NSLog (@ "execute2--> value:%@,address=%p,self=%p", Self.person, self.person,self);    };


(4) The method to be executed is extracted, and no circular reference is made:

    MyBlock3 = ^ (void) {        //This will not cause a circular reference, the method to be executed has been extracted        [weakself myblock3func];    };

-(void) myblock3func{    NSLog (@ "execute3--> value:%@,address=%p,self=%p", self.person,self.person,self);}


(5) When a block is not a property or variable of self, using self within a block does not loop the reference:

    When the block is not a self property, the block internally uses self and is not a circular reference    Animal *animal = [[Animal alloc] init];    Animal.animalblock = ^ (void) {        NSLog (@ "animal--> value:%@,address=%p,self=%p", self.person,self.person,self) ;    };


(6) block is called as follows:

    MyBlock1 ();    MyBlock2 ();    MyBlock3 ();    Animal.animalblock ();    


iOS development--block solutions that cause circular references

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.