I'm wondering if when a property that is set as weak gets cleared out via arc when it is not strongly referable, does any KVO registered for the key path pointing to that weak property fire? That would be a really handy feature but I'm unaware if this happens currently. Anyone know if it does, and if it doesn't by default can it be made to work?
You can't do that with ARC, but you can emulate this by associating an object with your iVar using objc_setAssociatedObject()
, it will be deallocated when the weak variable dies.
@interface WeakObjectDeathNotifier : NSObject
@end
@implementation WeakObjectDeathNotifier
- (void)dealloc
{
// the code that shall fire when the property will be set to nil
}
@end
You can build on top of that very elaborate notifiers, using NSNotificationCenter
or just custom blocks, depending on how heavily you rely on that for a specific ivar case or for lots of them.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With