Re: How do I know when my Core Data app is up and running?
Re: How do I know when my Core Data app is up and running?
- Subject: Re: How do I know when my Core Data app is up and running?
- From: "Arthur C." <email@hidden>
- Date: Tue, 03 Oct 2006 19:07:12 +0200
From: Paul Collins <email@hidden>
To: Arthur C. <email@hidden>
CC: email@hidden
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
_______________________________________________
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