我是使用块进行编程的新手段。我在我的监听级以下(不使用圆弧)代码:使用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