gpt4 book ai didi

iphone - 如何统计coredata(聚合)?

转载 作者:行者123 更新时间:2023-12-03 18:43:48 29 4
gpt4 key购买 nike

我正在学习核心数据,特别是聚合。

当前我想要做的事情:计算表中在某些条件上具有逆关系的多对关系的记录数

目前我正在这样做:

NSExpression *ex = [NSExpression expressionForFunction:@"count:" 
arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"ddname"]]];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ddtype == 'Home'"];
NSExpressionDescription *ed = [[NSExpressionDescription alloc] init];
[ed setName:@"countDDEvents"];
[ed setExpression:ex];
[ed setExpressionResultType:NSInteger16AttributeType];
NSArray *properties = [NSArray arrayWithObject:ed];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setPredicate:pred];
[request setPropertiesToFetch:properties];
[request setResultType:NSDictionaryResultType];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]];
[request setEntity:entity];
NSArray *results = [[self.currentAccount managedObjectContext] executeFetchRequest:request error:nil];
NSDictionary *dict = [results objectAtIndex:0];
NSLog(@"Average birthdate for female heroes: %@", [dict objectForKey:@"countDDEvents"]);

来自jeff lemarche。

编辑:我找到了我的解决方案

    NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"ddtype == 'Home'"];
[request setPredicate:pred];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"DDEvent" inManagedObjectContext:[self.currentAccount managedObjectContext]];
[request setEntity:entity];

NSError *error = nil;
NSUInteger count = [[self.currentAccount managedObjectContext] countForFetchRequest:request error:&error];

它运行良好。但我想一次执行更多此类类型的请求。所以我认为这不是获取计数的首选方式。

编辑:

所以我认为这种方法是合适的???

谁能告诉我更有效的首选方法。

谢谢。

最佳答案

我必须计算大约 10,000 个实体,在使用 countForFetchRequest 进行计算时,它大大降低了我的界面响应速度..

这是使用 NSExpression 实现此目的的一种方法:

- (NSUInteger) unfilteredFCsCount {

// Just the fetchRequest
NSFetchRequest *fetchRequest = [self unfilteredFCsFetchRequest];

[fetchRequest setResultType: NSDictionaryResultType];

// You can use any attribute of the entity. its important, because you are not counting
// the properties, but actually the entities
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"sortIndex_"]; // Does not really matter
NSExpression *maxExpression = [NSExpression expressionForFunction: @"count:"
arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: @"fcCount"];
[expressionDescription setExpression: maxExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];

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

NSUInteger fcCount = 0;
NSError *error = nil;
NSArray *results = nil;
results = [self.managedObjectContext executeFetchRequest: fetchRequest error: &error];
KSLog(KSLogLevelDebug, @"unfilteredFCsCount results: %@", results);

if([results count] > 0) {
NSNumber *count = [[results objectAtIndex: 0] objectForKey: @"fcCount"];
fcCount = [count intValue];
}

return fcCount;
}

关于iphone - 如何统计coredata(聚合)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3342540/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com