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: Rick Hoge <email@hidden>
- Date: Thu, 28 May 2009 16:17:57 -0400
Just a follow-up. It's not perfect, and I'll revisit the design, but
it does what I need for now.
Specifically my entity has a 'transformable' attribute for which the
values are a custom object class, and this object class has an ivar
that is an NSMutableDictionary. The user can modify entries in this
dictionary, and I would like this to cause the context to be flagged
as dirty and to have the custom object flagged to be updated in the
persistent store.
I got this all to work the way I need using the following strategy:
1) I used the dual representation (transient object + persistent
transformable data) for my custom object attribute, as described in
the Core Data docs on Non-Standard Persistent Attributes.
2) When the entity is fetched I create the transient object
representation from the persistent data attribute. When the entity is
fetched or inserted I add an observer for every entry in the
dictionary of the custom object.
3) If one of the observers is triggered, I invoke will/
didChangeValueForKey: for the persistent data attribute. This causes
the context to be flagged as dirty if a dictionary entry is changed.
4) I implement -willSave in the entity's class, and here assign the
persistent data attribute as shown in the Core Data doc example for
the "Delayed-Update Set Accessor". This assignment causes the data to
be rewritten to the disk store. The objects themselves are pretty
light-weight, so the fact that they will be re-written on every save
is not a huge deal.
This is a relatively small part of the whole app, so I can live for
now with the fact that changes to these dictionary entries won't
benefit from undo/redo. In fact the objects in question are shared
with a number of other apps that don't have undo/redo, so this will
not be too shocking to the users (although it would have been nice).
_______________________________________________
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