Skip to content

Instantly share code, notes, and snippets.

@davepeck
Last active August 29, 2015 13:56
Show Gist options
  • Save davepeck/9236473 to your computer and use it in GitHub Desktop.
Save davepeck/9236473 to your computer and use it in GitHub Desktop.
Strong/Weak/Strong Again
@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)
}
}];
}
@davepeck
Copy link
Author

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 to nil. Or maybe it is an issue, in which case you'd better re-form the sself strong reference.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment