Re: Core Data validateForUpdate
Re: Core Data validateForUpdate
- Subject: Re: Core Data validateForUpdate
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Tue, 22 Dec 2009 07:53:53 +0700
On 22 Dec 2009, at 04:50, Sean McBride wrote:
> On 12/20/09 3:19 PM, Gerriet M. Denkmann said:
>
>> What I try to accomplish:
>> SomeEntity has a property called "uniqueKey" and this is (no big
>> surprise) meant to be unique.
>>
>> When the "UniqueKey" column in my table view gets edited,
>> validateUniqueKey:error: gets called, I check the new value for
>> uniqueness and all is fine.
>>
>> But hitting the "+" button several times inserts several objects with
>> the same key (Default Value).
>
> As others have said, you probably want awakeFromInsert.
>
> Depending what kind of 'unique' value you want, you could create a
> unique string like this:
>
> CFUUIDRef uuid = CFUUIDCreate (kCFAllocatorDefault);
> if (uuid)
> {
> uuidStr = NSMakeCollectable (CFUUIDCreateString (kCFAllocatorDefault,
> uuid));
> CFRelease (uuid);
> }
>
> If you want/need to compare to other objects of the same entity type,
> then be warned that performing a fetch within awakeFromInsert is
> probably not a good idea. NSArrayController doesn't like it when you do that.
Thanks for all your help and suggestions.
What I ended up was: subclassing NSArrayController and overriding "newObject" to return a new object prefilled with some some nice values, including setting my uniqueKey to "undefined", "undefined_1", "undefined_2", etc.
This will break of course if the user (out of sheer perversity) has already created an "undefined_4".
But as "real" entries never will look like "undefined_..." there is no problem with eliminating duplicating entities later.
And, as I said, editiing "undefined_..." into some "real" value will (via: Validates immediately) catch real duplicates.
Kind reagards,
Gerriet.
_______________________________________________
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