CoreData crash/incorrect result using NSInMemoryStoreType and NSExpressionDescription with [NSExpression expressionForFunction:@"sum:"]

Originator:jkirsakmens
Number:rdar://16644607 Date Originated:17/04/2014
Status:Open Resolved:
Product:iOS SDK Product Version:7.1
Classification:Serious Bug Reproducible:Always
 
Summary:
If CoreData persistent Store is set to NSInMemoryStoreType, a fetch request which uses an NSExpressionDescription with [NSExpression expressionForFunction] (e.g. with @"sum:" to calculate an sum for a field) Core Data crashes (with [__NSCFNumber count]: unrecognized selector sent to instance) or fails to return array as a result (on empty database).   

The same fetch request with the same expression works correctly (without crash and returns desired result) when Core Data uses persistent store with NSSQLiteStoreType.

Steps to Reproduce:
Code: 

// Init fetch
NSFetchRequest *request = [Drink fetchRequest];
[request setResultType:NSDictionaryResultType];

// Set column for query result
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"numberOfDrinks"];

// Set query expression (SUM)
NSExpression *expression = [NSExpression expressionForFunction:@"sum:" arguments:[NSArray arrayWithObject:keyPathExpression]];

// Set expression result name and type
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"countOfDrinks"];
[expressionDescription setExpression:expression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];

[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];

// Execute fetch
NSError *error;
NSArray *result = [pContext executeFetchRequest:request error:&error];


Trace:
-[__NSCFNumber count]: unrecognized selector sent to instance 0xc436060
0   CoreFoundation                      0x0154e1e4 __exceptionPreprocess + 180
1   libobjc.A.dylib                     0x032868e5 objc_exception_throw + 44
2   CoreFoundation                      0x015eb243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
3   CoreFoundation                      0x0153e50b ___forwarding___ + 1019
4   CoreFoundation                      0x0153e0ee _CF_forwarding_prep_0 + 14
5   Foundation                          0x01a7f3cf +[_NSPredicateUtilities _getCommonTypeFor:] + 64
6   Foundation                          0x01a7f656 +[_NSPredicateUtilities sum:] + 155
7   Foundation                          0x0194e465 -[NSFunctionExpression expressionValueWithObject:context:] + 1260
8   CoreData                            0x011f93ba -[NSMappedObjectStore executeFetchRequest:withContext:] + 2602
9   CoreData                            0x011f891e -[NSMappedObjectStore executeRequest:withContext:error:] + 222
10  CoreData                            0x011807f2 -[NSPersistentStoreCoordinator executeRequest:withContext:error:] + 4466
11  CoreData                            0x0117df56 -[NSManagedObjectContext executeFetchRequest:error:] + 566
12 -- app method calling to perform fetch request (code mentioned above)


Expected Results:
App should not crash if Core Data stack is set with persistent store of NSInMemoryStoreType

Result of the request should not be nil and should return array with 1 member.

Actual Results:
On empty database (Core Data stack is set with persistent store of NSInMemoryStoreType) fetch request returns nil instead of array with 1 member.

If database contains data, Core Data crashes while performing [context executeFetchRequest:request error:&error] with exception: [__NSCFNumber count]: unrecognized selector sent to instance

Version:
iOS 7.1 (11D167)

Notes:
Related radars:
rdar://12795203
rdar://12021880

Configuration:
Xcode 5.1.1, iPhone 5S, iOS 7.1 (11D167)
or
Xcode 5.1.1, iPhone Simulator 7.1, iOS SDK 7.1

Comments

Still happens on Xcode 7.2, iOS 9.2

It's still happening, Any work around ?

By tarunkhurana1982 at March 14, 2016, 9:56 p.m. (reply...)

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!