简体   繁体   中英

Retain cycles when using addObserverForName:object:queue:usingBlock:

I am new to programming with block. I have following code (not using arc) in my Listener class:

- (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"];
        }];
}

and in my DoMyStuff class:

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

Can anyone tell me the right direction to solve the retain cycles? I have checked these two questions

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

but they did not use block inside another block, so, the solutions there don't work for me.

The problem here is that you are using [self.someProperty doSomething:myDictionary]; inside block thus retaining self. Note that using ivars will lead to retain cycles as it's the same as self->ivar.

Usually it looks like this (__weak/__unsafe_unretained is for ARC, __block for 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;
}];

There is nice library https://github.com/jspahrsummers/libextobjc which has @weakify/@strongify macroses for this(probably ARC only).

Also you should use ARC if possible(it is available from iOS 4, if I remember correctly and has __weak from iOS 5, which should be fine these days).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM