Re: Values set in awakeFromInsert have gone missing.
Re: Values set in awakeFromInsert have gone missing.
- Subject: Re: Values set in awakeFromInsert have gone missing.
- From: Chris Hanson <email@hidden>
- Date: Sun, 29 Jul 2007 19:02:17 -0700
I watched your movie and nothing jumps out at me as "just change this
and it will work" beyond what's already been mentioned.
I'll echo what Jim said, though: Your -init method is incorrect,
though you're absolutely right that Cocoa Scripting just assumes it
can send -init to any class you tell it about.
Here's what your -init method should look like:
- (id)init {
NSManagedObjectContext *appDelegateContext = [[NSApp
delegate] managedObjectContext];
NSEntityDescription *serviceEntity = [NSEntityDescription
entityForName:@"Service" inManagedObjectContext:appDelegateContext];
if (self = [self initWithEntity:serviceEntity
insertIntoManagedObjectContext:appDelegateContext]) {
// Any other -init code here.
}
return self;
}
I sent -initWithEntity:insertIntoManagedObjectContext: to "self"
rather than "super" intentionally above since it's the designated
initializer, and you may have overridden it for some other reason.
Also in your movie, I note that your -awakeFromInsert implementation
uses -setValue:forKey:. This is probably not what you want, since
you're setting *initial* values for your managed object. You should
generally use -setPrimitiveValue:forKey: in overrides of -
awakeFromInsert and -awakeFromFetch; that way they aren't treated as
undoable changes for the properties in question, but as their baseline
values.
Another thing I noticed is that you have a +initialize method that (a)
doesn't invoke [super initialize] and (b) isn't necessarily idempotent
if called multiple times (say because you have a subclass of your
CBService class with its own +initialize that does invoke [super
initialize]). Generally, your +initialize should work somewhat like
this:
+ (void)initialize {
[super initialize];
static BOOL initialized = YES;
if (initialized == NO) {
// MyClass-specific initialization here.
initialized = YES;
}
}
That way MyClass's superclass will also be guaranteed to have its
+initialize invoked before any of MyClass's initialization code runs,
and if a subclass of MyClass is sent +initialize, MyClass will still
only run its own initialization code once.
One more thing: Rather than using repeated invocations of +[NSDate
date] to initialize your created & last-modified date properties, you
might want to use a single invocation stashed in a local variable.
That way they're guaranteed to get the same value; with multiple
invocations and the order of the code as it is, you could wind up with
a creation date that's just slightly after the modification date...
-- Chris
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden