On 2009 Mar 11, at 18:39, Dave Fernandes wrote:

It helped me figure out what was changing by overriding willChangeValueForKey in my NSManagedObject subclass...

Dave, I just used your idea and was able to track down two of these phantom changes this afternoon.

At first, I was getting information overload, but then I modified your code so that it would only print when I was undoing. Details are explained in the code comments below. This is a great trick!


#define DEBUG_MANAGED_OBJECT_CHANGES
#ifdef DEBUG_MANAGED_OBJECT_CHANGES
#warning overrode willChangeValueForKey.  Do not ship this.
/*!
 @brief    In a Core Data document-based application, you
 often have the problem of the close button getting a dirty
 dot immediately after a new document is created or an old
 one loaded.  To find out what is changing, paste this
 code into the NSManagedObject subclass that you suspect
 might be changing, or even better, if you have a
 NSManagedObject parent class for all your subclasses
 paste it in there.  Define the constant above, compile,
 and run your app so that the dot gets dirty.  Then click
 Undo until the dot becomes clean as you watch the console.
 To your great delight, the answer will be magically printed.

 @details  Thanks to Dave Fernandes for this great idea!
*/
- (void)willChangeValueForKey:(NSString*)key
{
    if ([[[self managedObjectContext] undoManager] isUndoing]) {
        NSLog(@"%@ undid changed value for key: %@",
              [[self entity] name],
              key) ;
    }
    [super willChangeValueForKey:key];
}

#endif

One of problems I tracked down was that I was setting a managed object value in response to a notification, so this occurred after I invoked - clearChanges at the end of my document's initializer:

- (void)clearAllChanges {
    [self.managedObjectContext processPendingChanges] ;
    [self.managedObjectContext.undoManager removeAllActions] ;
    [self updateChangeCount:NSChangeCleared] ;
}

The solution, which I had worked out a couple weeks ago, is to invoke this with performSelector:afterDelay:0.0. It's maybe a little kludgey, but now since I know why it's needed I don't feel so bad about it.

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to