[ad_1]
+ (NSArray *)retrievePercent{
NSArray *percentResult = [NSArray arrayWithObjects:@"", nil];
NSFetchRequest *fetchRequest = [NTSmartRegionGPS MR_createFetchRequest];
NSString *entityName = [NTSmartRegionGPS MR_entityName];
NSManagedObjectContext *context = [NSManagedObjectContext MR_context];
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName
inManagedObjectContext:context];
//The question consequence shows the corresponding ssid worth
NSAttributeDescription *compactKeyDesc = [entity.attributesByName objectForKey:@"compactKey"];
//Sum ssid columns
NSExpression *compactKeyExpn = [NSExpression expressionForKeyPath:@"compactKey"];
NSExpression *countExpn = [NSExpression expressionForFunction:@"count:" arguments:@[compactKeyExpn]];
NSExpressionDescription *countExpnDesc = [[NSExpressionDescription alloc] init];
countExpnDesc.identify = @"rely";
countExpnDesc.expression = countExpn;
//TODO Utilizing NSInteger32AttributeType will trigger the proportion decision worth to be irregular, and the rationale to be investigated
countExpnDesc.expressionResultType = NSStringAttributeType;
//The tactic is as follows: rely() * 100/countOfEntity
//(It's discovered within the precise measurement that the division consequence shall be rounded. To get the proportion, multiply by 100 first)
NSUInteger countOfEntity = [NTSmartRegionGPS MR_countOfEntities];
if (countOfEntity <= 0) {
return percentResult;
}
NSExpression *multiplierValueExpn = [NSExpression expressionForConstantValue:@(100)];
NSExpression *multiplyExpn = [NSExpression expressionForFunction:@"multiply:by:" arguments:@[countExpn, multiplierValueExpn]];
NSExpression *sumValueExpn = [NSExpression expressionForConstantValue:@(countOfEntity)];
NSExpression *divideExpn = [NSExpression expressionForFunction:@"divide:by:" arguments:@[multiplyExpn, sumValueExpn]];
NSExpressionDescription *percentExpnDesc = [[NSExpressionDescription alloc] init];
percentExpnDesc.identify = @"p.c";
percentExpnDesc.expression = divideExpn;
//TODO Utilizing NSInteger32AttributeType will trigger the proportion decision worth to be irregular, and the rationale to be investigated
percentExpnDesc.expressionResultType= NSStringAttributeType;
fetchRequest.returnsObjectsAsFaults = false;
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = @[compactKeyDesc, countExpnDesc, percentExpnDesc];
fetchRequest.propertiesToGroupBy = @[@"compactKey"];
//Use rely outcomes to type in descending order
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"count" ascending:NO];
fetchRequest.sortDescriptors = @[sd];
@attempt {
percentResult = [NTSmartRegionGPS MR_executeFetchRequest:fetchRequest];
} @catch (NSException *exception) {
// Deadly Exception: NSInvalidArgumentException keypath rely not present in entity NTSmartRegionGPS
DDLogError(@"Fetch gps area p.c fail, exception:%@", exception.motive);
}
return percentResult;
}
NTSmartRegionGPS is an NSManagedObject object that inherits from CoreData.
The above perform implements group summation of data within the database, after which calculates the proportion.
Monitoring data by Crash exhibits the next traces will Crash:
percentResult = [NTSmartRegionGPS MR_executeFetchRequest:fetchRequest];
Crash data: Deadly Exception: ‘NSInvalidArgumentException’, motive: ‘keypath rely not present in entity ‘
Might anyone give a hand?;)
[ad_2]
