When are objects from a core data store initially fetched?

  • So I have this wonderful NSOutlineView bound to an NSTreeController.

    The NSTreeController uses core data to get some entities and
    everything looks great. Now I'm trying to save the state of the
    outline view (i.e. which nodes are expanded/collapsed) from session to
    session.

    Since I'm using Leopard and IB 3.0, I saw this handy checkbox
    ("Autosave Expanded Items"), so I thought "Great! More Cocoa magic
    saving me from coding" so I checked that, entered a string for the
    Autosave name, and thought that all was well. Except it wasn't.

    Then I discovered that I still needed to add some datasource methods,
    so I implemented:

    -(id)outlineView:(NSOutlineView *)outlineView persistentObjectForItem:
    (id)item

    and

    -(id)outlineView:(NSOutlineView *)outlineView itemForPersistentObject:
    (id)object

    Now that should work great, but it doesn't. Every time I expand a
    node, outlineView:persistentObjectForItem: is called, and I see the
    values being written into my plist file. When the app starts up, the
    onlineView:itemForPersisitentObject is called, one time for every
    value stored in the plist file, but alas this doesn't seem to help the
    outline view, as none of the nodes end up expanded.

    So then I read somewhere that I should walk the outline view myself
    when the application starts up and expand the items myself, since I'm
    using coredata and the data source methods don't work as one would
    think in such cases, so I tried that.

    At the applicationDidFinishLaunching notification I call a method in
    my NSOutlineView subclass that is designed to do just that.

    I retrieve my saved nodes from NSUserDefaults and start looping
    through the outline view looking for matching nodes. Now here is my
    big problem, for some reason (which I'm sure is obvious to someone),
    my NSOutlineView returns 0 for totalRows at this point. Once I exit
    the method, there are rows, but not immediately following the
    "applicationDidFinishLaunching" notification.

    So that brings me to my question. When exactly does an NSOutlineView
    that is bound to core data through NSTreeController get its data? I
    was sure that by the time "applicationDidFinishLaunching" fired that
    it would have it, but apparently not.
  • Try adding:

    NSError *error;
    BOOL ok = [treeController fetchWithRequest:nil merge:NO error:&error];

    This is described in the Core Data and Cocoa Bindings document:

    http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdBindings.html#/

    /apple_ref/doc/uid/TP40004194-DontLinkElementID_13

    There's a delay from connecting the managed object context to
    executing the initial fetch. It doesn't mention
    applicationDidFinishLaunching, but I believe it applies.

    Wil

    On Dec 20, 2007, at 4:02 PM, Lee Falin wrote:

    > So I have this wonderful NSOutlineView bound to an NSTreeController.
    >
    > The NSTreeController uses core data to get some entities and
    > everything looks great. Now I'm trying to save the state of the
    > outline view (i.e. which nodes are expanded/collapsed) from session
    > to session.
    >
    >
    > Since I'm using Leopard and IB 3.0, I saw this handy checkbox
    > ("Autosave Expanded Items"), so I thought "Great! More Cocoa magic
    > saving me from coding" so I checked that, entered a string for the
    > Autosave name, and thought that all was well. Except it wasn't.
    >
    > Then I discovered that I still needed to add some datasource
    > methods, so I implemented:
    >
    > -(id)outlineView:(NSOutlineView *)outlineView
    > persistentObjectForItem:(id)item
    >
    > and
    >
    > -(id)outlineView:(NSOutlineView *)outlineView
    > itemForPersistentObject:(id)object
    >
    >
    > Now that should work great, but it doesn't. Every time I expand a
    > node, outlineView:persistentObjectForItem: is called, and I see the
    > values being written into my plist file. When the app starts up, the
    > onlineView:itemForPersisitentObject is called, one time for every
    > value stored in the plist file, but alas this doesn't seem to help
    > the outline view, as none of the nodes end up expanded.
    >
    > So then I read somewhere that I should walk the outline view myself
    > when the application starts up and expand the items myself, since
    > I'm using coredata and the data source methods don't work as one
    > would think in such cases, so I tried that.
    >
    > At the applicationDidFinishLaunching notification I call a method in
    > my NSOutlineView subclass that is designed to do just that.
    >
    > I retrieve my saved nodes from NSUserDefaults and start looping
    > through the outline view looking for matching nodes. Now here is my
    > big problem, for some reason (which I'm sure is obvious to someone),
    > my NSOutlineView returns 0 for totalRows at this point. Once I exit
    > the method, there are rows, but not immediately following the
    > "applicationDidFinishLaunching" notification.
    >
    > So that brings me to my question. When exactly does an NSOutlineView
    > that is bound to core data through NSTreeController get its data? I
    > was sure that by the time "applicationDidFinishLaunching" fired that
    > it would have it, but apparently not.
  • That worked great, thanks!

    Lee

    On Dec 20, 2007, at 7:18 PM, William Turner wrote:

    > Try adding:
    >
    > NSError *error;
    > BOOL ok = [treeController fetchWithRequest:nil merge:NO error:&error];
    >
    > This is described in the Core Data and Cocoa Bindings document:
    >
    > http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdBindings.html#/

    > /apple_ref/doc/uid/TP40004194-DontLinkElementID_13
    >
    > There's a delay from connecting the managed object context to
    > executing the initial fetch. It doesn't mention
    > applicationDidFinishLaunching, but I believe it applies.
    >
    > Wil
    >
    > On Dec 20, 2007, at 4:02 PM, Lee Falin wrote:
    >
    >> So I have this wonderful NSOutlineView bound to an NSTreeController.
    >>
    >> The NSTreeController uses core data to get some entities and
    >> everything looks great. Now I'm trying to save the state of the
    >> outline view (i.e. which nodes are expanded/collapsed) from session
    >> to session.
    >>
    >>
    >> Since I'm using Leopard and IB 3.0, I saw this handy checkbox
    >> ("Autosave Expanded Items"), so I thought "Great! More Cocoa magic
    >> saving me from coding" so I checked that, entered a string for the
    >> Autosave name, and thought that all was well. Except it wasn't.
    >>
    >> Then I discovered that I still needed to add some datasource
    >> methods, so I implemented:
    >>
    >> -(id)outlineView:(NSOutlineView *)outlineView
    >> persistentObjectForItem:(id)item
    >>
    >> and
    >>
    >> -(id)outlineView:(NSOutlineView *)outlineView
    >> itemForPersistentObject:(id)object
    >>
    >>
    >> Now that should work great, but it doesn't. Every time I expand a
    >> node, outlineView:persistentObjectForItem: is called, and I see the
    >> values being written into my plist file. When the app starts up,
    >> the onlineView:itemForPersisitentObject is called, one time for
    >> every value stored in the plist file, but alas this doesn't seem to
    >> help the outline view, as none of the nodes end up expanded.
    >>
    >> So then I read somewhere that I should walk the outline view myself
    >> when the application starts up and expand the items myself, since
    >> I'm using coredata and the data source methods don't work as one
    >> would think in such cases, so I tried that.
    >>
    >> At the applicationDidFinishLaunching notification I call a method
    >> in my NSOutlineView subclass that is designed to do just that.
    >>
    >> I retrieve my saved nodes from NSUserDefaults and start looping
    >> through the outline view looking for matching nodes. Now here is my
    >> big problem, for some reason (which I'm sure is obvious to
    >> someone), my NSOutlineView returns 0 for totalRows at this point.
    >> Once I exit the method, there are rows, but not immediately
    >> following the "applicationDidFinishLaunching" notification.
    >>
    >> So that brings me to my question. When exactly does an
    >> NSOutlineView that is bound to core data through NSTreeController
    >> get its data? I was sure that by the time
    >> "applicationDidFinishLaunching" fired that it would have it, but
    >> apparently not.
    >
previous month december 2007 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