//weak-strong dance
Person *p1 = [[Person alloc] init];
__weak Person *wp1 = p1;
p1.myBlock = ^(){
__strong Person *sp1 = wp1;
NSLog(@"execute block");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",sp1);
});
};
p1.myBlock();
//结果:
2018-08-06 23:08:03.315110+0800 Demo[43110:8392352] execute block
2018-08-06 23:08:04.315584+0800 Demo[43110:8392352] <Person: 0x60000000a960>
2018-08-06 23:08:04.315797+0800 Demo[43110:8392352] -[Person dealloc]
//weak
Person *p1 = [[Person alloc] init];
__weak Person *wp1 = p1;
p1.myBlock = ^(){
NSLog(@"execute block");
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
NSLog(@"%@",wp1);
});
};
p1.myBlock();
//结果:
2018-08-06 23:09:11.973212+0800 Demo[43138:8394024] execute block
2018-08-06 23:09:11.973464+0800 Demo[43138:8394024] -[Person dealloc]
2018-08-06 23:09:13.062905+0800 Demo[43138:8394024] (null)
网友评论