Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does kvo happen when a weak property is set to nil by ARC? [duplicate]

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?

like image 257
Kevlar Avatar asked Sep 15 '13 04:09

Kevlar


1 Answers

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.

like image 88
JTAS Avatar answered Sep 22 '22 02:09

JTAS