参考:
http://www.cocoachina.com/ios/20170122/18601.html
https://www.jianshu.com/p/d911cd16c100
https://www.jianshu.com/p/492be28d63c4
//情况一:不泄露
- (void)case1
{
NSLog(@"case 1 Click");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
self.name = @"case 1";
});
}
//情况二:不泄露
- (void)case2
{
NSLog(@"case 2 Click");
__weak typeof(self) weakSelf = self;
[self.teacher requestData:^(NSData *data) {
typeof(weakSelf) strongSelf = weakSelf;
strongSelf.name = @"case 2";
}];
}
//情况三:泄漏
- (void)case3
{
NSLog(@"case 3 Click");
[self.teacher requestData:^(NSData *data) {
self.name = @"case 3";
}];
}
//情况四:不泄露
- (void)case4
{
NSLog(@"case 4 Click");
[self.teacher requestData:^(NSData *data) {
self.name = @"case 4";
self.teacher = nil;
}];
}
//情况五:不泄露
- (void)case5
{
NSLog(@"case 5 Click");
Teacher *t = [[Teacher alloc] init];
[t requestData:^(NSData *data) {
self.name = @"case 5";
}];
}
//情况六:不泄露
- (void)case6
{
NSLog(@"case 6 Click");
[self.teacher callCase6BlackEvent];
self.teacher.case6Block = ^(NSData *data) {
self.name = @"case 6";
//下面两句代码任选其一即可防止内存泄漏,self.teacher 或者 case6Block 置为空都可以打破 retain cycle
self.teacher = nil;
//self.teacher.case6Block = nil;
};
}
- (void)requestData:(void (^)(NSData *))block
{
self.caseBlock = block;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t) (0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(self.caseBlock != nil)
{
self.caseBlock(@"case");
}
//self.caseBlock = nil;//加上这句也可以防止内存泄漏
});
}
- (void)callCase6BlackEvent
{
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
if(self.case6Block != nil)
{
self.case6Block(@"case 6");
}
});
}