Re: managedObjectContext hasChanges
Re: managedObjectContext hasChanges
- Subject: Re: managedObjectContext hasChanges
- From: "Mu Lin" <email@hidden>
- Date: Thu, 5 Oct 2006 10:55:59 -0700
- Importance: normal
- Priority: normal
My simple test with a new core data app project (not document-based)
shows the bindings to hasChanges DOESN"T work. Further,
[managedObjectContext addObserver:self forKeyPath:@"hasChanges" ...]
doesn't work either, the
observeValueForKeyPath:ofObject:change:context: never gets called,
seems hasChanges is not observable.
Anyhow, I walked around the problem by registering for
NSManagedObjectContextObjectsDidChangeNotification and
NSUndoManagerDidChangeNotification and bind to the local copy of
hasChanges of appDelegate, IT WORKS.
code in the appDelegate.m is pasted here.
//////// new code to fix hasChanges of managedObjectContext
- (BOOL)hasChanges
{
return hasChanges;
}
- (void)setHasChanges:(BOOL)flag
{
return;
}
- (void)watchHasChanges:(NSNotification *)notification
{
if ( hasChanges != [[self managedObjectContext] hasChanges] )
{
[self willChangeValueForKey:@"hasChanges"];
hasChanges = [[self managedObjectContext] hasChanges];
[self didChangeValueForKey:@"hasChanges"];
}
}
/////// end of new code
- (NSManagedObjectContext *) managedObjectContext {
if (managedObjectContext != nil) {
return managedObjectContext;
}
NSPersistentStoreCoordinator *coordinator = [self
persistentStoreCoordinator];
if (coordinator != nil) {
managedObjectContext = [[NSManagedObjectContext alloc] init];
[managedObjectContext setPersistentStoreCoordinator:
coordinator];
}
////// new code to register for notification
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(watchHasChanges:)
name:NSManagedObjectContextObjectsDidChangeNotification
object:managedObjectContext];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(watchHasChanges:)
name:NSUndoManagerDidUndoChangeNotification object:
[managedObjectContext undoManager]];
////// end of new code
return managedObjectContext;
}
Mu
On Oct 4, 2006, at 2:55 PM, mmalc crawford wrote:
On Oct 4, 2006, at 1:37 PM, Mu Lin wrote:
> In a Core Data application, I've got the Save menu item bound to
> the saveAction: of my application delegate which works. I'd
like to
> have the menu disabled, however, until a change has been made. Is
> there any way to bind the enabled binding of the menu item to the
> context or model to do this, or do I have to write code to handle
> this?
Assuming that you're using the default configuration for a Cocoa
Core
Data Application, you can bind enabled to [App
Delegate].managedObjectContext.hasChanges
I found that the bind to the key "hasChanges" of
managedObjectContext doesn't work.
A trivial test with a new project shows this does work.
Are you using the default configuration for a *Cocoa Core Data
Application* (i.e. not a document-based application)?
Is the application delegate set correctly?
Have you changed the way the managed object context is set up/created?
mmalc
_______________________________________________
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