| 14 | | <$if Attribute.name == position_$> |
|---|
| 15 | | // position_ is a calculated value: @max(position_)+1. Calculate it the first time it's used. |
|---|
| 16 | | static BOOL fetching = NO; |
|---|
| 17 | | if (!result || [result longLongValue] == -1) { |
|---|
| 18 | | fetching = YES; |
|---|
| 19 | | NSFetchRequest *fetchObjectWithMaximumPosition = [[[NSFetchRequest alloc] init] autorelease]; |
|---|
| 20 | | [fetchObjectWithMaximumPosition setEntity:[self entity]]; |
|---|
| 21 | | [fetchObjectWithMaximumPosition setSortDescriptors:[NSArray arrayWithObject:[[[NSSortDescriptor alloc] initWithKey:@"position_" ascending:NO] autorelease]]]; |
|---|
| 22 | | [fetchObjectWithMaximumPosition setFetchLimit:1]; |
|---|
| 23 | | NSArray *objectWithMaximumPosition = [[self managedObjectContext] executeFetchRequest:fetchObjectWithMaximumPosition error:nil]; |
|---|
| 24 | | assert(objectWithMaximumPosition && [objectWithMaximumPosition count] < 2); |
|---|
| 25 | | result = [NSNumber numberWithLongLong:[objectWithMaximumPosition count] ? [[[objectWithMaximumPosition lastObject] valueForKey:@"position_"] longLongValue]+1 : 0]; |
|---|
| 26 | | [self setPosition_:result]; |
|---|
| 27 | | fetching = NO; |
|---|
| 28 | | } |
|---|
| 29 | | <$endif$> |
|---|