How do I know when my Core Data app is up and running?

  • When my program starts, all managed objects are fetched, which I can monitor
    using the awakeFromFetch method.
    Now I would like to get a notification when *all* managed objects are
    fetched, and the program is ready to run. At that moment, I would like to do
    some initialization code which requires that at least some, or preferably
    all managed objects are fetched and ready.

    So, is there such a notification, or a method like awakeFromFetch or
    applicationDidFinishLaunching which is executed at the right moment?

    Best regards,

    Arthur C.

    _________________________________________________________________
    Live Search, for accurate results! http://www.live.nl
  • On Oct 1, 2006, at 1:32 PM, Arthur C. wrote:

    > When my program starts, all managed objects are fetched, which I
    > can monitor using the awakeFromFetch method.
    > Now I would like to get a notification when *all* managed objects
    > are fetched, and the program is ready to run. At that moment, I
    > would like to do some initialization code which requires that at
    > least some, or preferably all managed objects are fetched and ready.
    >
    > So, is there such a notification, or a method like awakeFromFetch
    > or applicationDidFinishLaunching which is executed at the right
    > moment?

    Rather than await a notification, you can just start using Core Data
    methods.

    Fetching is not part of the initialization of Core Data. Core Data
    can be considered initialized when an NSManagedObjectContext is
    instantiated and receives a -setPersistentStoreCoordinator message.
    This is usually done in a -managedObjectContext accessor method,
    which you can override (if you subclass NSPersistentDocument) or
    modify (if you're using the "Core Data Application" template).

    Core Data objects are fetched when you want to do something with
    them. If you programmatically fetch all (or some) objects, they are
    returned in an NSArray you can do whatever you want with them.
    Fetching, like most actions in Core Data, requires an
    NSManagedObjectContext, so that ensures you have an "initialized Core
    Data."

    In a Core Data + Bindings application, there could be automatic
    fetching at application startup due to an NSArrayController in
    "Entity" mode in the MainMenu nib, or when an NSPersistentDocument is
    opened and contents displayed in a window.

    Depending or what your "some initialization code" does, it can
    probably be run at any reasonable time (such as in -
    applicationWillFinishLaunching) by triggering -managedObjectContext
    (such as by fetching or inserting objects, or perhaps just calling -
    managedObjectContext). If you need to work with the existing objects,
    you can execute a fetch request which returns them.

    If I've failed to help, try the Core Data FAQ in the Core Data
    Programming Guide.

    --Paul Collins
  • > From: Paul Collins <paul...>
    > To: Arthur C. <arthur_cw...>
    > CC: <cocoa-dev...>
    > Subject: Re: How do I know when my Core Data app is up and running?
    > Date: Sun, 1 Oct 2006 22:24:25 -0700
    >
    > On Oct 1, 2006, at 1:32 PM, Arthur C. wrote:
    >
    >> When my program starts, all managed objects are fetched, which I  can
    >> monitor using the awakeFromFetch method.
    >> Now I would like to get a notification when *all* managed objects  are
    >> fetched, and the program is ready to run. At that moment, I  would like to
    >> do some initialization code which requires that at  least some, or
    >> preferably all managed objects are fetched and ready.
    >>
    >> So, is there such a notification, or a method like awakeFromFetch  or
    >> applicationDidFinishLaunching which is executed at the right  moment?
    >
    > Rather than await a notification, you can just start using Core Data
    > methods.
    >
    > Fetching is not part of the initialization of Core Data. Core Data  can be
    > considered initialized when an NSManagedObjectContext is  instantiated and
    > receives a -setPersistentStoreCoordinator message.  This is usually done in
    > a -managedObjectContext accessor method,  which you can override (if you
    > subclass NSPersistentDocument) or  modify (if you're using the "Core Data
    > Application" template).
    >
    > Core Data objects are fetched when you want to do something with  them. If
    > you programmatically fetch all (or some) objects, they are  returned in an
    > NSArray you can do whatever you want with them.  Fetching, like most
    > actions in Core Data, requires an  NSManagedObjectContext, so that ensures
    > you have an "initialized Core  Data."
    >
    > In a Core Data + Bindings application, there could be automatic  fetching
    > at application startup due to an NSArrayController in  "Entity" mode in the
    > MainMenu nib, or when an NSPersistentDocument is  opened and contents
    > displayed in a window.

    Yes, that's right. In my case, it is an array controller in 'entity' mode.

    > Depending or what your "some initialization code" does, it can  probably be
    > run at any reasonable time

    Now for instance, I would like to set the selection in my table view to a
    previously stored value at program startup. This can be done by filtering
    objects from the array controller using a predicate, after which we send to
    the array controller 'setSelectedObjects' with the object(s) returned after
    filtering.

    The problem is that it is unknown when the array controller has its objects
    loaded. To detect this we simply call [[kPerson arrangedObjects] count],
    where kPerson is the array controller.

    The applicationWill/DidFinishLaunching is too early. When the objects are
    fetched (monitoring awakeFromFetch), sometimes the arraycontroller is
    loaded, sometimes it is not. So that is not the right time either. When the
    table views are initialized (monitoring the
    tableViewIsChangingNotification), same thing. Sometimes the array controller
    is loaded, sometimes it's not. The order in which things are executed isn't
    fixed.

    So, it still puzzles me how I should handle this properly (right now I
    explicitly wait for them to be loaded in the timer loop, which is not
    particularly elegant).

    Anyway, thanks for your reply,

    Arthur C.

    > (such as in - applicationWillFinishLaunching) by triggering
    > -managedObjectContext  (such as by fetching or inserting objects, or
    > perhaps just calling - managedObjectContext). If you need to work with the
    > existing objects,  you can execute a fetch request which returns them.
    >
    > If I've failed to help, try the Core Data FAQ in the Core Data  Programming
    > Guide.
    >
    > --Paul Collins

    _________________________________________________________________
    Live Search, for accurate results! http://www.live.nl
  • On Tue, 03 Oct 2006 19:07:12 +0200, "Arthur C." <arthur_cw...>
    said:

    >> In a Core Data + Bindings application, there could be automatic  fetching
    >> at application startup due to an NSArrayController in  "Entity" mode in the
    >> MainMenu nib, or when an NSPersistentDocument is  opened and contents
    >> displayed in a window.
    >
    > Yes, that's right. In my case, it is an array controller in 'entity' mode.
    >
    >> Depending or what your "some initialization code" does, it can  probably be
    >> run at any reasonable time
    >
    > Now for instance, I would like to set the selection in my table view to a
    > previously stored value at program startup.

    > The applicationWill/DidFinishLaunching is too early. When the objects are
    > fetched (monitoring awakeFromFetch), sometimes the arraycontroller is
    > loaded, sometimes it is not. So that is not the right time either. When the
    > table views are initialized (monitoring the
    > tableViewIsChangingNotification), same thing. Sometimes the array controller
    > is loaded, sometimes it's not. The order in which things are executed isn't
    > fixed.

    What I do is use delayed performance. This gives everything a chance to sync
    up - your next event comes in when the data is loaded. So, here's how my
    awakeFromNib goes:

    [self->entries prepareContent];
    [self performSelector:@selector(finishAwaking:) withObject:nil
    afterDelay:0.1];

    and then (in finishAwakening:):

    [self->entries setSelectionIndex: (ct-1)];
    [self->window makeKeyAndOrderFront:self];

    Unfortunately I get the Spinning Cursor of Death while waiting for the data
    to load, but that's a different matter. m.

    --
    matt neuburg, phd = <matt...>, <http://www.tidbits.com/matt/>
    A fool + a tool + an autorelease pool = cool!
    AppleScript: the Definitive Guide - Second Edition!
    <http://www.amazon.com/gp/product/0596102119>
  • > What I do is use delayed performance. This gives everything a chance to
    > sync
    > up - your next event comes in when the data is loaded. So, here's how my
    > awakeFromNib goes:
    >
    > [self-> entries prepareContent];
    > [self performSelector:@selector(finishAwaking:) withObject:nil
    > afterDelay:0.1];
    >
    > and then (in finishAwakening:):
    >
    > [self-> entries setSelectionIndex: (ct-1)];
    > [self-> window makeKeyAndOrderFront:self];

    Ah, that's exactly the kind of thing I was looking for. Although I would
    have expected Core Data to have its own 'finishAwakening' method... The line
    containing performSelector: afterDelay: will be sufficient for what I need.
    The notation in the other lines is unknown to me... what does
    'self->entries' mean? I was planning to invoke the array controller itself.

    > Unfortunately I get the Spinning Cursor of Death while waiting for the data
    > to load, but that's a different matter. m.

    Well, I'll survive the spinning cursor if it lasts only 0.1 seconds.

    Thanks for the reply,

    Arthur C.

    >
    > --
    > matt neuburg, phd = <matt...>, < http://www.tidbits.com/matt/ >
    > A fool + a tool + an autorelease pool = cool!
    > AppleScript: the Definitive Guide - Second Edition!
    > < http://www.amazon.com/gp/product/0596102119 >
    >
    >
    >

    _________________________________________________________________
    Live Search, for accurate results! http://www.live.nl
  • On Oct 4, 2006, at 4:51 PM, Arthur C. wrote:

    > Ah, that's exactly the kind of thing I was looking for. Although I
    > would have expected Core Data to have its own 'finishAwakening'
    > method...

    That's because you are phrasing the question wrong. The array
    controller is doing the fetching, so you want to know when array
    controller is done fetching the data. The controller layer is doing
    the work here, not Core Data.

    You originally asked:

    > Now I would like to get a notification when *all* managed objects
    > are fetched, and the program is ready to run. At that moment, I
    > would like to do some initialization code which requires that at
    > least some, or preferably all managed objects are fetched and ready.

    What kind of work do you want to do?

    Depending upon what you want to do, waiting for the array controller
    to fetch may not be necessary, or the right answer.

    Jim
  • On or about 10/4/06 1:51 PM, thus spake "Arthur C." <arthur_cw...>:

    > The notation in the other lines is unknown to me... what does
    > 'self->entries' mean? I was planning to invoke the array controller itself.

    self->entries *is* the array controller.

    > Well, I'll survive the spinning cursor if it lasts only 0.1 seconds.

    If it lasts only 0.1 seconds there won't be any spinning cursor. But that is
    not what the 0.1 means. You'll be called back when you're called back. m.

    --
    matt neuburg, phd = <matt...>, http://www.tidbits.com/matt/
    pantes anthropoi tou eidenai oregontai phusei
    AppleScript: the Definitive Guide - Second Edition!
    http://www.amazon.com/gp/product/0596102119
    Take Control of Word 2004, Tiger, and more -
    http://www.takecontrolbooks.com/tiger-customizing.html
    Subscribe to TidBITS! It's free and smart. http://www.tidbits.com/
  • On Oct 4, 2006, at 1:51 PM, Arthur C. wrote:

    > Ah, that's exactly the kind of thing I was looking for. Although I
    > would have expected Core Data to have its own 'finishAwakening'
    > method... The line containing performSelector: afterDelay: will be
    > sufficient for what I need.
    >
    <http://developer.apple.com/documentation/Cocoa/Conceptual/CoreData/Articles
    /cdBindings.html#//apple_ref/doc/uid/TP40004194-SW1
    >

    mmalc
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