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: Mike Zornek <email@hidden>
- Date: Tue, 31 Jul 2007 01:21:45 -0400
On 7/29/07 10:02 PM, "Chris Hanson" <email@hidden> wrote:
> 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.
Doing this change seems to have fixed my problem. Thanks.
> 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.
I'm going to doing a lot of Undo testing in the next few days and will take
a look then. Thanks for the tip.
> 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.
Thanks for the tip. But for the archive, watch out for the typo. I think it
should be:
static BOOL initialized = NO;
> 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...
I learned this a little while back. :-D For this code snippet it isn't
really that important but I've made the change.
Thanks for all the help! Worked on AppleScript all day. Have most of it
working; maybe 75%. A few bugs and hard things to implement (image/pdf
properties) remain but I'm getting there...
~ Mike
--
Work: http://ClickableBliss.com
Play: http://MikeZornek.com
_______________________________________________
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