Three scenarios in which IOS can easily be cited as "circular references"

Source: Internet
Author: User

One, Parent-child mutual hold, delegate mode

  @interface ftappcentermainviewcontroller () { } &NBSP; < Code class= "Java Color1" > @property (weak,nonatomic) uitableview* Mytableview; @end
The Mytableview in this use the weak modifier.
1 @property(nonatomic, weak)  id<ftactionsheetdelegate>delegate;

"Recommended Method":

Child only the object of the parent is the weak type:

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

second, block

"Case":

Look at the following code:

?
2 " Span style= "FONT-SIZE:14PT;" >3 5 < Span style= "FONT-SIZE:14PT;" >typedef void (^ Requestnavicallback) (Nsinteger navicode,nsinteger httpcode,nserror * error); @interface Ftnavimanager:nsobject { " Span style= "FONT-SIZE:14PT;" > @property (nonatomic, strong)    requestnavicallback navicallback;
This is a request navigation class, the class attribute holds the Requestnavicallback, at this time, if the requestnavicallback again holds self, inevitably causes the circular reference.

"Recommended Method":

If there is a circular reference, the compiler prompts for a warning.

If the object does not hold a block object, a circular reference is not generated. If the object holds a block object, it is defined when the block references self:

?
1 __weak typeof(self) weakSelf = self;

Third, Nstimer

"Case":

?
1 2 3 4 5 6 @interfaceFtKeepAlive : NSObject{    NSTimer*              _keepAliveTimer; // 发送心跳timer}//实现文件_keepAliveTimer = [NSTimer scheduledTimerWithTimeInterval:_expired target:self selector:@selector(keepLiveStart) userInfo:nil repeats:YES];

The class holds the _keepalivetimer,_keepalivetimer and holds the self, causing a circular reference.

"Recommended Method":

Nstimer will hold the object, so: Before deleting the object, the timer's Invalidate method needs to be set.

?
1 2 3 4 -(void)stopKeepAlive{    [_keepAliveTimer invalidate];    _keepAliveTimer = nil;}

Three scenarios in which IOS can easily be cited as "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.