-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRetainCycleViewController_two.m
More file actions
113 lines (83 loc) · 3.22 KB
/
RetainCycleViewController_two.m
File metadata and controls
113 lines (83 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//
// RetainCycleViewController_two.m
// Test_ everyday
//
// Created by prliu on 2017/3/21.
// Copyright © 2017年 prliu. All rights reserved.
//
#import "RetainCycleViewController_two.h"
typedef void(^myBlock)(int);
@interface RetainCycleViewController_two ()
-(void)gogogo_two;
@property (nonatomic,strong)dispatch_queue_t queue;
@property (nonatomic,weak)myBlock myb;
@property (nonatomic,strong)myBlock myb2;
@property (nonatomic,copy) myBlock myb3;
@end
@implementation RetainCycleViewController_two
-(void)gogogo_two{
NSLog(@"RetainCycleViewController_two fun gogogo");
}
- (void)viewDidLoad {
[super viewDidLoad];
UIImage *image = [UIImage imageNamed:@"WechatIMG3"];
/*
self.queue = dispatch_queue_create("", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(self.queue, ^{
//模拟内存告急
//NSMutableArray*arr = [NSMutableArray array];
for (int i = 0; i<999999; i++) {
[arr addObject:image];
}
//模拟循环引用测试1,系统block 不会造成循环
self.view.layer.contents = (id)image.CGImage;
});
*/
/*
dispatch_queue_t queue2 = dispatch_queue_create("", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue2, ^{
//模拟循环引用测试2,系统block 不会造成循环
self.view.layer.contents = (id)image.CGImage;
});
*/
//自定义block
/*
_myb = ^{
//模拟循环引用测试2,自定义的block,如果修饰词为strong 不会造成循环
self.view.layer.contents = (id)image.CGImage;
};
*/
/*
_myb2 = ^{
//模拟循环引用测试3,自定义的block,如果修饰词为strong,copy 会造成循环
self.view.layer.contents = (id)image.CGImage;
};
*/
/*
_myb = ^(int i){
NSLog(@"模拟循环引用测试3,自定义的block,如果修饰词为weak,assign都不会造成循环.但是用weak时,会警告Assigning block literal to a weak variable; object will be released after assignment");
//模拟循环引用测试3,自定义的block,如果修饰词为weak,assign都不会造成循环,但是weak有警告
self.view.layer.contents = (id)image.CGImage;
};
*/
//正确的解决方式
__weak RetainCycleViewController_two *weakSelf = self;
_myb2 = ^(int i){
NSLog(@"模拟循环引用测试3,自定义的block,如果修饰词为strong,copy 会造成循环");
//模拟循环引用测试3,自定义的block,如果修饰词为strong,copy 会造成循环
weakSelf.view.layer.contents = (id)image.CGImage;
};
_myb2(1);
// Do any additional setup after loading the view from its nib.
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
@end