如何计数在coredata(聚合)?

编程入门 行业动态 更新时间:2024-10-28 06:31:33
本文介绍了如何计数在coredata(聚合)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在学习核心数据,特别是在汇总工作。

当前我要做什么:计算表中的记录数在某些标准上具有反比关系的多对多关系。

目前我正在这样做:

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:@DDEventinManagedObjectContext:[self.currentAccount managedObjectContext]]; [request setEntity:entity]; NSArray * results = [[self.currentAccount managedObjectContext] executeFetchRequest:request error:nil]; NSDictionary * dict = [results objectAtIndex:0]; NSLog(@女性英雄的平均出生日期:%@,[dict objectForKey:@countDDEvents]);

来自 jeff lemarche。

EDIT :我发现我的解决方案为

NSFetchRequest * request = [[NSFetchRequest alloc] init]; NSPredicate * pred = [NSPredicate predicateWithFormat:@ddtype =='Home']; [request setPredicate:pred]; NSEntityDescription * entity = [NSEntityDescription entityForName:@DDEventinManagedObjectContext:[self.currentAccount managedObjectContext]]; [request setEntity:entity]; NSError * error = nil; NSUInteger count = [[self.currentAccount managedObjectContext] countForFetchRequest:request error:& error];

它工作正常。但我想一次做更多的类型的请求。所以我认为这不是一个首选的计数方法。

EDIT :

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

所以任何人都可以告诉我更有效率的做这个的首选方法。 p>

感谢。

解决方案

Jeff LaMarche只是使用这个例子。在实践中,这种需求非常普遍,键值编码具有内置宏来处理它和其他常见的收集操作。

请参阅:键值编程指南:集合和数组运算符

在这种情况下,您将在您的谓词中使用 @count 运算符。

当然,手动调整自己的表达式可以精确控制谓词,但是操作符处理这种任务的80%。

I am learning core data and particularly working on aggregation.

Current what I want to do : count the number of records from the table which is in to-many relationship with inverse relationship on some criteria.

Currently I am doing this :

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"]);

Its from jeff lemarche.

EDIT : and I have found my solution as

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];

It is working nicely .But I want to do more request of such type at a time . So i think this can't be a preferred way of getting the count .

EDIT :

So I think the approach would be the appropriate one ????

So can anyone tell me more efficient an preferred way of doing this .

Thanks .

解决方案

Jeff LaMarche is just using this as a simple example. In practice, this need is so common that Key-Value Coding has a built in macro to handle it and other common collection operations.

See: The Key-Value Programming Guide: Set and Array Operators

In this case you would use the @count operator in your predicate.

Of course, hand tuning your own expression gives you fine control over your predicates but the operators handle 80% of such task.

更多推荐

如何计数在coredata(聚合)?

本文发布于:2023-11-22 05:14:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1616104.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:coredata

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!