Re: Observing Core Data
Re: Observing Core Data
- Subject: Re: Observing Core Data
- From: Jim Correia <email@hidden>
- Date: Tue, 12 Sep 2006 08:52:13 -0400
On Sep 12, 2006, at 8:18 AM, Julio Cesar Silva dos Santos wrote:
5) Now imagine the following situation: The first time the user
launches the application, the Persistent Data Store is empty and I
would like to detect this and add all the names to the
NSArrayController in order to have them available to the user;
6) I thought that adding an observer to Key Path
"arrangedObjects.@count" would give me this answer and I would do
the following:
if ([[MyController content] count] == 0) {
NSEntityDescription * ed = [NSEntityDescription
entityForName:@"VideoSystem" inManagedObjectContext:[self
managedObjectContext]];
VideoSystemMO * vmo = [[VideoSystemMO alloc]
initWithEntity:ed insertIntoManagedObjectContext:[self
managedObjectContext]];
[vmo setVideoSystemName:@"NTSC"];
[MyController addObject:vmo];
}
[...]
10) Then my question is: how can I detect (and when) that the
Persistent Data Store is loaded and one of its arrays are empty?
The array controller is the glue that binds your model to your user
interface. Since you want to automatically populate the model when
certain conditions are met (or aren't, in this case), this is not a
controller/view problem. It is strictly a model problem.
What kind of application are you building?
If you are building a CoreData document based application, the
documentation tells you how to do this.
<http://developer.apple.com/documentation/Cocoa/Conceptual/
NSPersistentDocumentTutorial/00_Introduction/chapter_1_section_1.html>
If you are building a CoreData application with a central store, and
are not using NSPersistentDocument, the basic recipe is
Early in the startup process (-applicationDidFinishLaunching: for
example) execute a fetch request for the entity VideoSystem. If you
get back 0 instances, pre-populate with the default values.
If you want to avoid re-populating with the default values on the
next launch if the user intentionally deletes all of the factory
default instances, you can record that you've already done this work
one in the store's metadata area.
Jim
_______________________________________________
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