Core Data, how to refresh views after object save?

Discussion in 'Mac Programming' started by gigantor, Oct 12, 2008.

  1. gigantor macrumors newbie

    gigantor

    Joined:
    Oct 12, 2008
    Location:
    Melbourne, Australia
    #1
    Hi,

    I have a basic core data app with a view using core data bindings to display fields of a managed object. When the object is saved via APIs the bound data used core data bindings does not refresh.

    How do I update/refresh views following a core data object save ?
     
  2. Littleodie914 macrumors 68000

    Littleodie914

    Joined:
    Jun 9, 2004
    Location:
    Rochester, NY
    #2
    It's been a while, but...

    It really depends on the class of the view you're updating. An NSView, for example, would be best updated calling:

    Code:
    [myView setNeedsDisplay];
    While an instance of NSTableView would be updated calling:

    Code:
    [myTableView reloadData];
    :)
     
  3. gigantor thread starter macrumors newbie

    gigantor

    Joined:
    Oct 12, 2008
    Location:
    Melbourne, Australia
    #3
    Thanks for that but I was wondering if there was a way to cause the refresh from the Core Data layer upwards rather than from the GUI View layer.

    I guess I am looking for a way for Core Data managed objects to notify interested views etc when data in a managed object changes, ie in my case, saving an updated managed object would cause views with bindings to that managed object to get notified to refresh their displays.

    Does anyone know of an existing mechanism on Cocoa or core data to do this ?
     
  4. HiRez macrumors 603

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    Is the saving of a Core Data object separate from updating bindings? Don't the bindings get updated when the managed object changes in the context instead of when it gets saved back to storage from the context (and then undone if changes are not saved)?

    Anyway, if you are setting data programmatically, can you wrap the changes in NSObject's -willChangeValueForKey: and -didChangeValueForKey: calls to trigger propogation of the changes to your view objects? I dunno, that might work.
     
  5. gigantor thread starter macrumors newbie

    gigantor

    Joined:
    Oct 12, 2008
    Location:
    Melbourne, Australia
    #5
    Hi,

    I got the issue solved. My program had inadvertently created a separate NSManagedObjectContext object seperate from the instance used by the interface builder generated classes.

    Once I updated the code to use the context created in the Interface Builder class then everything worked as expected. Updating and saving a managed object programmatically caused the GUI to update automatically as expected.

    Regards,
    Gigantor
     

Share This Page