managedObjectContext hasChanges

  • Hi, All, I found that the bind to the key "hasChanges" of
    managedObjectContext doesn't work.

    I did exactly the same as Mmalcolm suggested.  Any suggestions?

    FROM : mmalcolm crawford
    DATE : Mon Jun 20 22:58:33 2005

    On Jun 20, 2005, at 10:47 AM, Jeff LaMarche 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
  • 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
  • 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
    >
    >
  • On Oct 5, 2006, at 10:55 AM, Mu Lin wrote:

    > My simple test with a new core data app project (not document-based)
    > shows the bindings to hasChanges DOESN"T work.
    >
    In your application you appear to be binding a *button*, not a menu
    item...

    mmalc
  • On Oct 5, 2006, at 9:43 PM, mmalc crawford wrote:

    >
    > On Oct 5, 2006, at 10:55 AM, Mu Lin wrote:
    >
    >> My simple test with a new core data app project (not document-
    >> based) shows the bindings to hasChanges DOESN"T work.
    >>
    > In your application you appear to be binding a *button*, not a menu
    > item...
    >
    > mmalc

    Thanks, Mmalcolm, the binding to menu item does work, but the same
    binding to a NSButton doesn't, is there any specific reason for this
    behavior?

    Mu
previous month october 2006 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