Last active
August 29, 2015 13:56
-
-
Save davepeck/9236473 to your computer and use it in GitHub Desktop.
Strong/Weak/Strong Again
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@property (strong, nonatomic) id observer; | |
// ... | |
- (void)startListening | |
{ | |
__weak MyClass *wself = self; | |
self.observer = | |
[[NSNotificationCenter defaultCenter] addObserverForName:@"foo" object:nil queue:nil usingBlock:^(NSNotification *note){ | |
MyClass *sself = wself; | |
if (sself != nil) | |
{ | |
// Look ma, no retain cycles (provided all I reference is sself) | |
} | |
}]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Weak references are inherently volatile;
wself
could go away between any two lines of code. Maybe that's not an issue in your case, in which case (since it's an ARC weak ref) you'll just send lots of messages tonil
. Or maybe it is an issue, in which case you'd better re-form thesself
strong reference.