Re: Observing Core Data
Re: Observing Core Data
- Subject: Re: Observing Core Data
- From: Julio Cesar Silva dos Santos <email@hidden>
- Date: Tue, 12 Sep 2006 09:18:46 -0300
I will try to give a more accurate (and real) example of what I am
trying to accomplish:
1) My app has an entity called VideoSystem, which has only one
property videoSystemName (an NSString). This entity is defined in a
custom class called VideoSystemMO.h/.m;
2) This entity is bound to an NSArrayController, which has a content
method that returns an array of entities inside it;
3) If I have some video system names (NTSC, PAL, PAL-M, PAL-N, etc.)
all I have to do is create a new VideoSystemMO class, using its
method setVideoSystemName to one of these strings above and add it to
the NSArrayController using the method addObject. That works fine;
4) If I want to know how many VideoSystems there are in
NSArrayController I can do one of these things:
- Ask [[NSArrayController content] count];
- Ask [NSArrayController
valueForKeyPath:@"arrangedObjects.@count"];
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];
}
7) Obviously I will have the system names in a dictionary and I will
add all of them (this is only a simplification of the process);
8) In my tests I figured out that the observer is only triggered when
there is already a value (or values) in NSArrayController, so if it
is empty (as in the first run) nothing happens;
9) I have tried to use NSLog in applicationDidFinishLaunching but
even when there is a value in controller (using step 4) it logs 0 so
it is not reliable (apparently the Persistent Data Store is not
loaded at this time).
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?
Julio Cesar Silva dos Santos
email@hidden
Blogjective-C
http://www.jcs.santos.nom.br/wp
FetchYourLyrics
http://www.jcs.santos.nom.br/fyl/fyl.html
On 11/09/2006, at 20:33, Miguel Sanchez wrote:
and just to cover all bases, remember that you can set default
values for ATTRIBUTES within the modeling tool itself, if that's
what you're trying to do
- Miguel
On Sep 7, 2006, at 7:56 PM, Matt Neuburg wrote:
On Thu, 7 Sep 2006 19:48:59 -0300, Julio Cesar Silva dos Santos
<email@hidden> said:
Hi,
I am working on an application that uses Core Data and I am
trying to
implement a (quite) simple feature: Some entities have default
values
so I would like to add these values when the user launches the app
for the first time.
Not quite sure what it means for an *entity* to have a *value*,
but if you
mean what I think you might mean, perhaps this will help:
<http://developer.apple.com/documentation/Cocoa/Conceptual/
ModelObjects/Arti
cles/moInitialization.html>
m.
--
matt neuburg, phd = email@hidden, <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>
_______________________________________________
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