-
Notifications
You must be signed in to change notification settings - Fork 924
Automatic removal of observation for not retained observed objects. #94
Comments
Swizzling |
Hey @nlutsenko, I've come here from #67. I'd feel a bit nervous about the swizzling of dealloc too, especially as ARC does some strange stuff there as well (i.e. ARC automatically calls [super dealloc] somehow). Still, might work. Your second point sounds great, but I'm not 100% sure how it would work. Specifically doing the I tried a proof-of-concept here: https://gist.github.com/joerick/a0b87607e69071fa6a3c In my example the observer knows the person dealloc'd because the observer's dealloc gets called. Here we're relying on
I'm not 100% confident these are reliable assumptions, the second feels a bit race-conditiony. Any other ideas of how to call the |
That proof of concept actually looks fine, since the assumptions here are correct. I would expect associated objects being cleared before KVO is checked on dealloc, since it literally needs to deconstruct all dependents (all ivars/instance objects) before being able to check for KVO. One strong reference to the observer - this is expected and is required for this to work. |
Looks like using long keypaths to KVO-observing creates chain on observers and also manages of automagically unsubscribing objects in this chain while chain mutations like this:
And idea is to make something like this: [self.KVOControllerNonRetaining observe:self
keyPath:@"viewModel.title"
options:(NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew)
block:^(id observer, id object, NSDictionary<NSString *,id> * change)
{
//
}]; => [self.KVOControllerNonRetaining observe:self.KVOControllerNonRetaining
keyPath:@"observer.viewModel.title"
options:(NSKeyValueObservingOptionInitial|NSKeyValueObservingOptionNew)
block:^(id observer, id object, NSDictionary<NSString *,id> * change)
{
//
}]; But looks like exactly this not works because of |
Here is good answer, explaining KVO keypath observing chain implementation: http://stackoverflow.com/a/16533561/440168 |
@k06a, that's a great idea! So we can avoid the dealloc stuff entirely, if we observe across a |
Can any one please tell whether this enhancement is completed or still under development? |
Recently tried different type of automatic unobserving KVO: Swizzled I don't really like swizzling those 3 methods, but this allowed to forget about unobserving and allowed to observe UPDATE: Updated urls |
Just point out: associated objects are cleaned up automatically on NSObject's dealloc. It's fine for single key. but for key path, it's a little later. |
@SolaWing exactly. Associated object for each object in keyPath works, but horrors a little bit :) |
@nlutsenko what do you think? |
This is a follow-up issue for #48 for discussions and actual implementation of automatic removal of any observation registered via KVOController when the observed objects are not retained in any way.
The story here is that unless you let KVOController retain the objects - all of the observation becomes as fragile as it is with vanilla Cocoa KVO APIs.
Taking into account that we all would love to observe things in a "weak" way - there are 2 ways one could implement such a thing:
dealloc
and unregister registered observers orKVOController
s there. Sounds like less than ideal solution, but this will work just as fine. It feels very very fragile, but is a supported use case for ObjC runtime.KVOController
. As well as add all of the KVO observation with extra prefix path component whenever KVOController is registering an observer. Then store this instance via associated objects on the observed object.This will allow both weak reference to the original object, as well as to know the moment where we need to unregister KVO, as associated objects are cleaned up automatically on dealloc.
Do you have an idea/suggestion/comment? Great! Please post it here!
Do you want to take a chance on implementing this? Amazing! Pull requests are very very welcome, and I'll do my best to review/guide/help/drive any implementation of this.
The text was updated successfully, but these errors were encountered: