Re: Getting added object with KVO
Re: Getting added object with KVO
- Subject: Re: Getting added object with KVO
- From: Bill Northcott <email@hidden>
- Date: Thu, 13 Jul 2006 09:41:09 +1000
On 13/07/2006, at 5:01 AM, email@hidden wrote:
On 12 jul 2006, at 00.43, Hannes Petri wrote:
Hello!
I'm using KVO to observe an array controller, and when an object is
added, a view should be changed.
Now I want a pointer to the object that was added. How should I do?
Apples documentation says that I should get the index of the new
object, by querying the changes-dict with the key
NSKeyValueChangeIndexesKey, but that returns null.
What am I doing wrong? Is it possible to achieve what I want?
I have been struggling with some similar issues recently. Although
not the same as you are describing, I think they may be related.
I came to the conclusion that my problems were caused because add: is
run asynchronously (in its own thread). This means that if you do
[someArrayController add:self];
and then try to access the new object in the immediately following
code, it does not exist.
The fix I used was to change the line to
NSManagedObject *newObject = [someArrayController newObject];
This runs synchronously because the add: has to finish before the new
object can be returned to the caller. Also you have the new object
without needing to grovel around for it.
This also solved a problem I had with undo. The add: even if called
within the method for another add: is regarded as a separate undo
manager event. Using newObject makes them all part of the same event
which was what I wanted.
Also, regarding a comment made by some one else. I don't think you
should override add:. In my experience, nastiness happens if you
do. The docs tell you to mess with addObject: instead.
Bill Northcott
_______________________________________________
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