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