2014-12-02 30 views
1

我是使用塊進行編程的新手段。我在我的監聽級以下(不使用圓弧)代碼:使用addObserverForName時保留週期:object:queue:usingBlock:

- (void)someBlock:((void)^(NSDictionary *)myDictionary)myBlock 
{ 
    __block Listener *weakSelf = self; 
    weakSelf = [[NSNotificationCenter defaultCenter] 
        addObserverForName:@"MyNotification" 
           object:nil 
           queue:nil 
          usingBlock:^(NSNotification *note) 
     { 
      //--- Here have the retain cycles 
      myBlock(note.userInfo); 
      [[NSNotificationCenter defaultCenter] removeObserver:weakSelf 
                  name:@"MyNotification"]; 
     }]; 
} 

,並在我的DoMyStuff類:

... some code 
Listener *myListener = [[[Listener alloc] init] autorelease]; 
[myListener someBlock:((void)^(NSDictionary *)myDictionary)myBlock{ 
    [self.someProperty doSomething:myDictionary]; 
}]; 

誰能告訴我正確的方向,解決了保留週期? 我已經檢查了這兩個問題

  1. "Correct management of addObserverForName:object:queue:usingBlock:"
  2. "Why doesn't Remove Observer from NSNotificationCenter:addObserverForName:usingBlock get called"

,但他們沒有使用塊的其他的塊中,因此,這些解決方案有沒有爲我工作。

+0

我的回答應該可能有幫助。 – 2014-12-02 13:40:52

+1

您應該注意''-addObserverForName:object:queue:usingBlock:'不會返回對'self'的引用,而是一個指向作爲觀察者的不透明對象的指針。 – 2014-12-02 14:14:05

+0

是什麼讓你覺得有一個保留週期? – newacct 2014-12-03 06:28:21

回答

0

這裏的問題是,您正在使用[self.someProperty doSomething:myDictionary];內部塊因此保持自我。請注意,使用ivars將導致保留週期,因爲它與自我> ivar相同。

通常它看起來像這樣(__weak/__ unsafe_unretained是ARC,__block的MRR)

__weak ClassName *weakSelf = self; 
[SomeClass someMethodWithBlock:^{ 
    // use weakSelf here; if you want to make sure that self is alive throughout whole block, do something like ClassName *strongSelf = weakSelf; 
}]; 

有很好的圖書館https://github.com/jspahrsummers/libextobjc其中有@ weakify/@ strongify macroses這個(大概只有ARC)。

如果可能的話,你也應該使用ARC(如果我沒有記錯的話,它可以從iOS 4獲得,並且iOS 5有__weak,這些日子應該沒問題)。

+0

非常感謝。我已經嘗試過,但對我的情況不起作用。我已經爲操作提出了一些意見。 – dreamhome 2014-12-03 09:00:41