Re: KVO/dealloc problems
Re: KVO/dealloc problems
- Subject: Re: KVO/dealloc problems
- From: "Jeremy Higgs" <email@hidden>
- Date: Fri, 28 Apr 2006 20:07:44 +1000
I too would be interested in seeing how to do this. I have a mutable
array which populates a tableview (through bindings). However, when an
attribute of one of the objects in the array is modified, I'd like the
controller to be notified so it can replace some dictionary entries.
e.g.
Controller ---> Mutable array -> object -> attribute (*)
(*) denotes being observed by the controller.
I have this working via KVO at the moment, but the objects are removed
from the mutable array independent of the controller (since the array
is bound to the tableview), so the controller has no idea when the
object is removed/dealloc'd via the interface.
Another option I saw was to observe the array for changes and work
from there, but it seems quite kludgy, and by the time the observer
receives the message, it appears that the affected object has already
been dealloc'd (so I can't remove the observer, anyway!).
Can anyone suggest how this might be done?
Thanks
On 4/24/06, Duncan Campbell <email@hidden> wrote:
Hi list,
I'm having some problems removing a KVO. I'm creating an object, and
adding it as an observer like so (this is the parent class i want to
observe):
- (id)init
{
if (self = [super init])
{
_obj = [[MyObject alloc] init];
[self addObserver:_obj forKeyPath:@"somevalue"
options:NSKeyValueObservingOptionNew context:NULL];
}
return self;
}
and in my dealloc:
- (void)dealloc
{
[self removeObserver:_obj forKeyPath:@"somevalue"];
[_obj release];
_obj = nil;
[super dealloc];
}
and obviously, i have implemented - (void)
observeValueForKeyPath:ofObject:change:context: in the "MyObject" class.
My problem is that when the dealloc above happens, I get an error:
"An instance 0x36e7a0 of class <SomeClass> is being deallocated while
key value observers are still registered with it."
so it would seem that I need to remove the observer before dealloc is
called.
Can anyone suggest where in fact i should be removing the observer?
(Note that the "MyObject" object does not know anything about the
parent class - so i cannot do the add/remove observer stuff there).
Cheers for any help.
D.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden