Core Data: how to prevent document being dirtied when chaging attributes?

  • Hi all,

    In a Core Data app, how should one model a property of an object that a)
    should be saved as part of the document b) should not participate in
    undo c) should not dirty the document when changed and d) can be easily
    bound to?

    (Here's a lame example: consider a custom view MyView, it exposes an
    NSColor binding to draw its background.  If my model has a colour
    property then my view can bind to it.  The colour is important, and so
    gets saved as part of the persistent store.  But let's say that changing
    the colour is not so important that it should dirty the document.  How
    do I do that?  Perhaps a better example is window size, I want to save
    the size of the window as part of the document, but I don't want
    changing the window size to make the document dirty.)

    Thanks,

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
  • On 17.01.2008, at 16:35, Sean McBride wrote:

    > Hi all,
    >
    > In a Core Data app, how should one model a property of an object
    > that a)
    > should be saved as part of the document b) should not participate in
    > undo c) should not dirty the document when changed and d) can be
    > easily
    > bound to?

    disabling undo-registration (as described in "Undo Management" <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdUsingMOs.html#//apple_ref/doc/uid/TP40001803-207821-TPXREF148
    >) should do the trick. I see no reason
    why disabling undo-registration should break bindings in any way.

    Cheers,

    felix
  • On 1/17/08 5:34 PM, Felix Franz said:

    >> In a Core Data app, how should one model a property of an object
    >> that a)
    >> should be saved as part of the document b) should not participate in
    >> undo c) should not dirty the document when changed and d) can be
    >> easily
    >> bound to?
    >
    > disabling undo-registration (as described in "Undo Management" <http://
    > developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles/
    > cdUsingMOs.html#//apple_ref/doc/uid/TP40001803-207821-TPXREF148
    >> ) should do the trick. I see no reason
    > why disabling undo-registration should break bindings in any way.

    Felix,

    Thanks for the link.  I had tried something like that with limited
    success, but was missing the critical processPendingChanges.

    So is it typical to do such a thing in an NSManagedObject subclass' KVC
    setter?  Like so:

    - (void)setName:(NSString*)value_
    {
    NSManagedObjectContext* moc = [self managedObjectContext];
    NSUndoManager* undoMan = [moc undoManager];

    [moc processPendingChanges];
    [undoMan disableUndoRegistration];

    [self willChangeValueForKey:@"name"];
    [self setPrimitiveValue:value_ forKey:@"name"];
    [self didChangeValueForKey:@"name"];

    [moc processPendingChanges];
    [undoMan enableUndoRegistration];
    }

    Cheers,

    --
    ____________________________________________________________
    Sean McBride, B. Eng                <sean...>
    Rogue Research                        www.rogue-research.com
    Mac Software Developer              Montréal, Québec, Canada
previous month january 2008 next month
MTWTFSS
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      
Go to today