Re: CoreData - re-faulting object and removing self-observers results in messages being sent to zombie
Re: CoreData - re-faulting object and removing self-observers results in messages being sent to zombie
- Subject: Re: CoreData - re-faulting object and removing self-observers results in messages being sent to zombie
- From: Bill Bumgarner <email@hidden>
- Date: Tue, 2 Aug 2005 17:30:52 -0700
On Aug 2, 2005, at 1:55 PM, Jim Correia wrote:
I have a CoreData based application. One of my NSManagedObject
subclasses observes itself (for a variety of reasons, but just this
in on the example) so it can keep a lastModified time for each object.
Generally, observation of self is discouraged [though it should work,
I think]. In any case, you are going to end up with a boatload of
observers and lots of cycles spent maintaining the observers as
managed objects are created and destroyed. And there are some issues
in relation to faults, as you have discovered.
Instead, why not make the lastModifiedTime a dependent key on the
rest of the object's keys?
In particular:
- make lastModifiedTime dependent upon all keys that need to update
the lastModifiedTime
- override willChangeValueForKey: to:
- call super first
- check to see if the key is lastModifiedTime
- if it is, then use primitive KVC to set the lastModifiedTime
to now
The above should work. At least, Ben and I think it will work.
There are some subtle details to this. It works because the
dependent invocation of willChangeValueForKey: for the
lastModifiedTime key does not actually indicate that a new value is
about to be set. Even when it is invoked in the case where
traditional KVC is about to change that key directly, the value set
in the implementation of willChangeValueForKey: (as described above)
will be correctly lost -- will not appear in either the undo stack
(because the super invocation of willChangeValueForKey: has already
recorded the snapshot) or the object (because the set will happen
after the willChange...:).
Alternatively, you could punt on the dependent key stuff and just
make willChangeValueForKey: always update the lastModifiedTime,
regardless of key (via the primitive KVC as described above).
Unfortunately, there isn't a "hey -- if that key changes,
independently recompute the value of this key" hook. There probably
should be. Someone should file a feature request, if they agree.
b.bum
_______________________________________________
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