okeyang's blog

ARC与非ARC下的Weak-Strong Dance

| Comments

ARC

在使用block过程中,经常会遇到retain cycle的问题,例如:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:_observer];
}

- (void)loadView
{
  [super loadView];
          
  _observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"testKey"
                                                                object:nil
                                                                 queue:nil
                                                            usingBlock:^(NSNotification *note) {
      [self dismissModalViewControllerAnimated:YES];  
  }];
}

在block中用到了self,self会被block retain,而_observer会copy一份该block,就是说_observer间接持有self,同时当前的self也会retain _observer,最终导致self持有_observer,_observer持有self,形成retain cycle

对于在block中的retain cycle,在2011 WWDC Session #322 (Objective-C Advancements in Depth)有一个解决方案weak-strong dance,很漂亮的名字。其实现如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:_observer];
}

- (void)loadView
{
  [super loadView];
          
  __weak TestViewController *wself = self;
  _observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"testKey"
                                                                object:nil
                                                                 queue:nil
                                                            usingBlock:^(NSNotification *note) {
      TestViewController *sself = wself;
      [sself dismissModalViewControllerAnimated:YES];
  }];
}

在block中使用self之前先用一个__weak变量引用self,导致block不会retain self,打破retain cycle,然后在block中使用wself之前先用__strong类型变量引用wself,以确保使用过程中不会dealloc。简而言之就是推迟对self的retain,在使用时才进行retain。这有点像lazy loading的意思。

注:iOS5以下没有__weak,则需使用__unsafe_unretained

非ARC

在非ARC环境中,显然之前的使用的__weak__unsafe_unretained将会是无效的,那么我们需使用另外一种方法来代替,这里就需要用到__block

__block在ARC和非ARC中有点细微的差别(Automatic Reference Counting : Blocks):

  • 在ARC中,__block会自动进行retain
  • 在非ARC中,__block不会自动进行retain

因此首先要注意的一点就是用__block打破retain cycle的方法仅在非ARC下有效,下面是非ARC的weak-strong dance

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
- (void)dealloc
{
  [[NSNotificationCenter defaultCenter] removeObserver:_observer];
  [_observer release];

  [super dealloc];
}

- (void)loadView
{
  [super loadView];
          
  __block TestViewController *bself = self;
  _observer = [[NSNotificationCenter defaultCenter] addObserverForName:@"testKey"
                                                                object:nil
                                                                 queue:nil
                                                               ngBlock:^(NSNotification *note) {
      [bself retain];
      [bself dismissModalViewControllerAnimated:YES];
      [bself release];
  }];
}

将self赋值为__block类型变量,在非ARC中__block类型变量不会进行retain,从而打破retain cycle,然后在使用bself前进行retain,以确保在使用过程中不会dealloc。

Comments