Several scenarios in which IOS can easily cause "circular references"

Source: Internet
Author: User

In Reading, the author sums up a few scenarios where the iOS platform can easily cause circular references:

Parent-child Mutual holding and entrusting mode
Second, block
Third, Nstimer
Four, like the self if the array. can also cause circular references

five, adding properties using categories Parent-child Mutual holding and entrusting mode

"Case":

@interface Ftappcentermainviewcontroller ()
{
}

@property (weak,nonatomic) uitableview* Mytableview;
@end
The Mytableview in this box uses the weak modifier.

@property (nonatomic, weak)  id<ftactionsheetdelegate>delegate;

"Recommended Method":

Child only Parent's object is weak type:

@property (nonatomic, weak)  id<ftactionsheetdelegate>delegate;

Second, block

"Case":

Look at the following code:

typedef void (^requestnavicallback) (Nsinteger navicode,nsinteger httpcode,nserror * error);
@interface ftnavimanager:nsobject
{
}
@property (nonatomic, strong)   Requestnavicallback Navicallback;
This is a class that requests navigation, and the class attribute holds the Requestnavicallback, and if requestnavicallback holds self again, it necessarily causes a circular reference.

there are, of course, some implicit (indirect) circular references. Objecta holds OBJECTB,OBJECTB holding block. Then invoking Objecta self in the block can result in an indirect circular reference.

"Recommended Method":

If there is a display circular reference, the compiler prompts the warning that it is best to use the Weak-strong dance technique when the block references self.

Weak-strong Dance Technology

Detailed weak-stong-dance technology, view here three, Nstimer

"Case":

@interface ftkeepalive:nsobject
{
    nstimer*              _keepalivetimer;//Send heartbeat Timer
}
//implementation file
_ Keepalivetimer = [Nstimer scheduledtimerwithtimeinterval:_expired target:self selector: @selector (Keeplivestart) Userinfo:nil Repeats:yes];

Class holds the _keepalivetimer,_keepalivetimer and holds self, resulting in a circular reference.

"Recommended Method":

Nstimer will hold the object, so: Before deleting the object, you need to invalidate the timer to the method.

-(void) stopkeepalive{
    [_keepalivetimer invalidate];
    _keepalivetimer = nil;
}
four, like adding self to the array. can also cause circular references five, adding properties using categories

For example: There is a Class A, add property P dynamically to a. If you refer to Class A again in P, it is easy to create a circular reference

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.