IOS loop reference

Source: Internet
Author: User

IOS loop reference

I asked about circular references during the interview today, so I looked at it. It turns out that Block is easy to cause circular references. Today, we will briefly introduce some circular references.

What is circular reference?

Circular references can be simply understood as follows: object A references object B, and object B references object. The two maintain a reference of each other. As a result, the count is not 0 at any time, and both cannot be released.

Method of Generating cyclic reference:

1. NSTimer

For example, I declare an NSTimer Timer class in a class. The NSTimer Method

+ (NSTimer *)scheduledTimerWithTimeInterval:(NSTimeInterval)seconds                                     target:(id)target                                   selector:(SEL)aSelector                                   userInfo:(id)userInfo                                    repeats:(BOOL)repeats

When we call our own attributes or methods in aSelector (for example, we call our own self. class), then when we instantiate this class and then let it be released several seconds later, we will find that the aSelector in time will be executed all the time. Now let's take a simple analysis:

If timer does not call the invalidate method, it will not stop because of the release of the class. For a class instance, it considers that the timer has not been executed, so there is no chance to enter dealloc. Reference cyclically and wait for each other. Therefore, we can manually call the invalidate method of NSTimer before the class instance is released. There is also the following code:

__weak ClassA *weakSelf = self;timer = [NSTimer scheduledTimerWithTimeInterval:1.0f target:weakSelf selector:@selector(startTimer) userInfo:nil repeats:YES];

 

Therefore, we must pay attention to the invalidate time of NSTimer when using nstdate.

2. Block

For example, if we define a block and use it as an attribute variable of a class, a circular reference will be generated when we call our own attributes or methods in this block.

Solution to circular reference: declare a new self variable through _ weak to replace the original self. In this way, the weak reference self can be used in the block to prevent strong reference to self in the block.

3. delegate

Generally, we use assign to assign values when declaring delegate, because the value assignment attribute does not increase the count.

If a UITableViewController object a obtains the ownership of UITableView object B through retain, the delegate of UITableView object B is a. If the delegate is retain, there is basically no chance to release these two objects.

This is a simple introduction, because there is no computer here, and it is not convenient for example demonstration. Make up your computer. Here we will record it first.

Terminate ------------------------------------------------------------------------------------

I am also very grateful to anyone who can attach some examples.

 

Related Article

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.