RE: [Q] "auto"-incrementing integer attribute in awakeFromInsert
RE: [Q] "auto"-incrementing integer attribute in awakeFromInsert
- Subject: RE: [Q] "auto"-incrementing integer attribute in awakeFromInsert
- From: "Jon C. Munson II" <email@hidden>
- Date: Thu, 19 Mar 2009 10:39:04 -0400
- Organization: JTA Enterprises LLC
Namaste!
OK, I hit on a solution...
Again, thanks Sean for your reply, further digging following that route got
me what I needed.
I found this post:
http://www.cocoabuilder.com/archive/message/cocoa/2005/9/5/145766
This gave me the template I needed to implement this (abbreviated):
-(void)awakeFromInsert
{
[self performSelector:@selector(setAttribute) withObject:nil
afterDelay:0];
}
-(void)setAttribute
{
int num = 0;
NSArray *a;
NSManagedObjectContext *moc = ...;
NSFetchRequest *request = ...;
[request setEntity:[self entity]];
NSError *e = ...;
a = [moc executeFetchRequest...];
if( a != nil )
{
num = [[a valueForKeyPath:@"@max.attribute"] integerValue];
}
num ++;
[self setAttribute:[NSNumber numberWithInt:num]];
}
Which worked wonderfully.
Thanks to mmalc for posting the original reply (referenced above) which
provided the template I was looking for.
Peace, Love, and Light,
/s/ Jon C. Munson II
> -----Original Message-----
> From: cocoa-dev-bounces+jmunson=email@hidden [mailto:cocoa-dev-
> bounces+jmunson=email@hidden] On Behalf Of Jon C. Munson II
> Sent: Thursday, March 19, 2009 10:10 AM
> To: 'Sean McBride'; 'Cocoa Developers'
> Subject: RE: [Q] "auto"-incrementing integer attribute in awakeFromInsert
>
> Namaste!
>
> Thank you for your answer.
>
> I did some digging and found that the real issue is the call for
> processPendingChanges that apparently is called via the executeFetch.
> According to what I read this is standard behavior.
>
> So, I'd have to say it isn't a "bug" in this case.
>
> Relative to the performSelector you suggested, I am not able to locate a
> sample that shows how to use that in conjunction with awakeFromFetch in
> order to do what I'd like. Do you know of a sample somewhere that shows
> such use?
>
> Also, would a better solution be to create a custom -addEntity method that
> fetches the next value, then calls the NSArrayController's add (or a more
> relevant -add), then updates the relevant attribute?
>
> To me it makes far more sense to put such stuff in the awakeFromInsert, so
> I'd consider the above a kludge, however, if that, in this case, is what
> needs doing...
>
> Many thanks in advance!
>
> Peace, Love, and Light,
>
> /s/ Jon C. Munson II
>
> > -----Original Message-----
> > From: Sean McBride [mailto:email@hidden]
> > Sent: Wednesday, March 18, 2009 12:17 PM
> > To: email@hidden; 'Cocoa Developers'
> > Subject: Re: [Q] "auto"-incrementing integer attribute in
> awakeFromInsert
> >
> > On 3/18/09 11:52 AM, Jon C. Munson II said:
> >
> > >What happens is the attribute is set with the correctly incremented
> > number.
> > >However, the tableview that is used to manage this entity displays two
> > rows
> > >every time a new entity is inserted.
> >
> > Fetching from awakeFromInsert is discouraged. There are some bugs/
> > problems. If you have an NSArrayController that will make use of the
> > object you're inserting (seems to be your case), it will receive
> > duplicate KVO notifications. Please file a bug with a repro case to
> > Apple.
> >
> > >So, my question is, how do I resolve the two-row error, or, is there a
> > >better way?
> >
> > You're best bet is using performSelector:withObject:afterDelay: in
> > awakeFromInsert, that way you're not fetching in awakeFromInsert.
> >
> > --
> > ____________________________________________________________
> > Sean McBride, B. Eng email@hidden
> > Rogue Research www.rogue-research.com
> > Mac Software Developer Montréal, Québec, Canada
>
>
> _______________________________________________
>
> 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
_______________________________________________
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