Re: Triggering change notifications when modifying custom object ivars in core data app
Re: Triggering change notifications when modifying custom object ivars in core data app
- Subject: Re: Triggering change notifications when modifying custom object ivars in core data app
- From: Quincey Morris <email@hidden>
- Date: Thu, 28 May 2009 11:16:56 -0700
On May 28, 2009, at 06:03, Rick Hoge wrote:
I have a core data (doc-based) app in which one of the entities
includes a custom object as a persistent attribute. The custom
object class implements NSCoding, and these objects are saved to the
core data store with no problem (they are read back correctly etc).
Instance variables of the custom object are exposed in the user
interface, and can be edited by the user.
The problem I have is that changes to instance variables of these
custom objects are not reported to the managed object context, so
the core data document is not flagged as dirty and, even if I force
a save by adding an entity and invoking save, the modified custom
object is not saved (if I quit and reload the old ivar values are
loaded).
Well, yeah. The problem is that you're using something with the
behavior of an entity (your custom object) as an attribute. That is,
you want your object graph to include your custom objects (for the
purpose of monitoring their changes), but you haven't implemented your
data model that way. Typically, Core Data *attributes* are immutable
(like NSNumber or NSString) for exactly that reason. The mutability of
your custom object suggests that it should be an entity, not an
attribute. [Sorry, not sure if that's clear. I'm trying to say the
same thing 3 different ways.]
Is there a reason you can't make your custom object a Core Data
entity, and expose its properties (the troublesome instance variables)
as attributes of that entity?
It's not impossible that the willChange/didChange approach can be made
to work, but this sort of thing tends to really crap out in the face
of undo. Consider what happens when you change an instance variable of
a custom object, if the object is already in the undo history from a
previous change. Suddenly, undoing changes ... won't.
_______________________________________________
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