Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could use Core Data Aggregate Functions to get the maximum displayOrder value. </p> <p>Something like: </p> <pre><code>- (NSNumber *)nextDisplayOrder { NSManagedObjectContext *context = self.managedObjectContext; NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:context]; NSFetchRequest *request = [[NSFetchRequest alloc] init]; [request setEntity:entityDescription]; [request setResultType:NSDictionaryResultType]; NSExpression *keyExpression = [NSExpression expressionForKeyPath:@"displayOrder"]; NSExpression *maxExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyExpression]]; NSExpressionDescription *description = [[NSExpressionDescription alloc] init]; [description setName:@"maxDisplayOrder"]; [description setExpression:maxExpression]; [description setExpressionResultType:NSFloatAttributeType]; [request setPropertiesToFetch:[NSArray arrayWithObject:description]]; NSError *error = nil; NSArray *results = [context executeFetchRequest:request error:&amp;error]; if (results.count == 1) { NSNumber *maxDisplayOrder = [[results objectAtIndex:0] valueForKey:@"displayOrder"]; return [NSNumber numberWithFloat:[maxDisplayOrder floatValue] + 1.f]; } else { NSLog(@"Could not get next displayOrder. Error: %@", error); } return nil; } </code></pre> <p>Some additional notes:</p> <ul> <li>use an integer rather than a float for discrete values like the displayOrder</li> <li>this boilerplate code can be written in 2 lines with <a href="https://github.com/magicalpanda/MagicalRecord" rel="nofollow">MagicalRecord</a>, so you might want to check that out</li> <li><a href="http://brianflove.com/2012/09/26/core-data-aggregate-functions/" rel="nofollow">more info on aggregate functions</a></li> </ul>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload