Re: removeObserver:forKeyPath:context: fails; but removeObserver:forKeyPath: works?!
Re: removeObserver:forKeyPath:context: fails; but removeObserver:forKeyPath: works?!
- Subject: Re: removeObserver:forKeyPath:context: fails; but removeObserver:forKeyPath: works?!
- From: Quincey Morris <email@hidden>
- Date: Wed, 11 Mar 2015 23:28:06 +0000
On Mar 11, 2015, at 15:03 , Sean McBride <email@hidden> wrote:
> It's here:
I just spent a few minutes with it. A couple of things:
— I don’t think it’s a very good idea to do this (slightly abbreviated from your source):
> - (void)bind:(NSString*)inBindingName toObject:(id)inObservableObject withKeyPath:(NSString*)inKeyPath options:(NSDictionary*)inOptions {
> [inObservableObject addObserver:self forKeyPath:inKeyPath options:0 context:context];
> [super bind:inBindingName toObject:inObservableObject withKeyPath:inKeyPath options:inOptions];
> }
The standard implementation of bindings, which you’re using when you invoke super, *also* adds the exact same observation as you just did (though with a different or nil context, presumably).
When you invoke super in ‘unbind', I guess it’s using the context-less form of removeObserver.
I don’t have an explanation of exactly why that might fail, but it certainly seems a lot like the conditions for bug I talked about earlier. You have the same object observing the same property of the same target twice.
I would suggest you simply don’t do this to the binding, or better still don’t use a binding here at all. But of course my opinion here is basically valueless, since I don’t know the design of your app.
— I commented out the super ‘bind’ in the above method to see what would happen. After also commenting out the various asserts, I ended up with this log output:
> •BIND: ptr: 0x610000140bb0, tag: 1, name: "crosshairsTransform", context: 123456
> •BIND: ptr: 0x610000140c60, tag: 2, name: "crosshairsTransform", context: 123456
> •BIND: ptr: 0x610000140b00, tag: 3, name: "crosshairsTransform", context: 123456
> •OBSERVE: ptr: 0x610000140bb0, tag: 1: keyPath: "selection.crosshairs", context: 123456
> •OBSERVE: ptr: 0x610000140c60, tag: 2: keyPath: "selection.crosshairs", context: 123456
> •OBSERVE: ptr: 0x610000140b00, tag: 3: keyPath: "selection.crosshairs", context: 123456
> •UNBIND: ptr: 0x610000140c60, tag: 2, name: "crosshairsTransform", context: 123456, hasInfo: 0
> •UNBIND: ptr: 0x610000140b00, tag: 3, name: "crosshairsTransform", context: 123456, hasInfo: 0
> •DEALLOC: ptr: 0x610000140b00, tag: 3
> •DEALLOC: ptr: 0x610000140c60, tag: 2
> •OBSERVE: ptr: 0x610000140bb0, tag: 1: keyPath: "selection.crosshairs", context: 123456
which I think is the behavior you’re looking for. (Of course, the app crashed shortly after that, because the object controller didn’t have any content, or some such consequence of the missing binding.)
To my mind, that confirms that the multiple-identical-observation scenario is the problem.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden