我是使用塊進行編程的新手段。我在我的監聽級以下(不使用圓弧)代碼:使用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];
}];
誰能告訴我正確的方向,解決了保留週期? 我已經檢查了這兩個問題
- "Correct management of addObserverForName:object:queue:usingBlock:"
- "Why doesn't Remove Observer from NSNotificationCenter:addObserverForName:usingBlock get called"
,但他們沒有使用塊的其他的塊中,因此,這些解決方案有沒有爲我工作。
我的回答應該可能有幫助。 – 2014-12-02 13:40:52
您應該注意''-addObserverForName:object:queue:usingBlock:'不會返回對'self'的引用,而是一個指向作爲觀察者的不透明對象的指針。 – 2014-12-02 14:14:05
是什麼讓你覺得有一個保留週期? – newacct 2014-12-03 06:28:21