NSDictionaryResultType expression ignoring newly inserted objects

I want the maximum field value in the kernel data, so I set the following query:

// Create fetch NSFetchRequest *fetch = [[NSFetchRequest alloc] init]; [fetch setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:context]]; [fetch setResultType:NSDictionaryResultType]; // Expression for Max ID NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"myNumbericID"]; NSExpression *minExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]]; NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init]; [expressionDescription setName:@"maxID"]; [expressionDescription setExpression:minExpression]; [expressionDescription setExpressionResultType:NSDoubleAttributeType]; [fetch setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]]; // Execute the fetch. double theID = 0; NSError *error; NSArray *objects = [context executeFetchRequest:fetch error:&error]; if (objects && [objects count] > 0) { theID = [((NSNumber *)[[objects objectAtIndex:0] valueForKey:@"maxID"]) doubleValue]; } 

However, it would seem that it does not take into account any new objects that were inserted into the context before distribution. Is this the way it should work? Does it work only on objects in the store?

I can not find links to it in the docs.

Thanks,

Mike

+5
core-data nsfetchrequest
source share
3 answers

I had a response from Apple Developer Forums, and it was confirmed that it does not account for unsaved changes.

+7
source share

This behavior is documented (poorly) in [NSFetchRequest setIncludePendingChanges:]. YES is not supported by NSDictionaryResultType. This means that you cannot use NSExpressionDescription with unsaved changes.

+1
source share

FYI, you can skip the entire NSExpression section and just continue fetching, and then use the collection operators for the maximum value.

 theID = [objects valueForKeyPath:"@max.myNumbericID"]; 
0
source share

All Articles