提问人:Bingerz 提问时间:10/9/2022 最后编辑:Bingerz 更新时间:10/10/2022 访问量:61
核心数据崩溃:在实体中找不到 NSInvalidArgumentException 键路径计数
Core Data crash: NSInvalidArgumentException keypath count not found in entity
问:
+ (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 query result displays the corresponding ssid value
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.name = @"count";
countExpnDesc.expression = countExpn;
//TODO Using NSInteger32AttributeType will cause the percentage resolution value to be abnormal, and the reason to be investigated
countExpnDesc.expressionResultType = NSStringAttributeType;
//The method is as follows: count() * 100/countOfEntity
//(It is found in the actual measurement that the division result will 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.name = @"percent";
percentExpnDesc.expression = divideExpn;
//TODO Using NSInteger32AttributeType will cause the percentage resolution value to be abnormal, and the reason to be investigated
percentExpnDesc.expressionResultType= NSStringAttributeType;
fetchRequest.returnsObjectsAsFaults = false;
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = @[compactKeyDesc, countExpnDesc, percentExpnDesc];
fetchRequest.propertiesToGroupBy = @[@"compactKey"];
//Use count results to sort in descending order
NSSortDescriptor *sd = [NSSortDescriptor sortDescriptorWithKey:@"count" ascending:NO];
fetchRequest.sortDescriptors = @[sd];
@try {
percentResult = [NTSmartRegionGPS MR_executeFetchRequest:fetchRequest];
} @catch (NSException *exception) {
// Fatal Exception: NSInvalidArgumentException keypath count not found in entity NTSmartRegionGPS
DDLogError(@"Fetch gps region percent fail, exception:%@", exception.reason);
}
return percentResult;
}
NTSmartRegionGPS 是继承自 CoreData 的 NSManagedObject 对象。 上述函数实现了数据库中记录的组求和,然后计算百分比。
Crash 的监控记录显示以下行将 Crash:
percentResult = [NTSmartRegionGPS MR_executeFetchRequest:fetchRequest];
崩溃信息:致命异常:“NSInvalidArgumentException”,原因:“在实体中找不到键路径计数”
这段代码在我的iPhone12(真机)中测试过,它可以正常输出百分比结果(当数据库为空/有单条数据/有多条数据时),所以不知道为什么用户(%1-%2)会遇到崩溃。
谁能伸出援手;)?
答:
0赞
Ely
10/9/2022
#1
a 中的排序键必须是模型中核心数据实体的一部分。NSSortDescriptor
NSFetchRequest
如果您不将 fetch 请求与 一起使用,您还可以使用实体自定义类中的计算属性作为排序键。必须在核心数据模型中注册此自定义类。NSFetchedResultController
评论