gpt4 book ai didi

iphone - 通过核心数据的不同计数,NSExpression 到 NSFetchedResultsController

转载 作者:太空狗 更新时间:2023-10-30 03:35:51 28 4
gpt4 key购买 nike

目前正在使用核心数据。我有一张表,我试图在其中按照以下方式检索信息:

SELECT item, COUNT(*) FROM myTable GROUP BY item;

为了产生这种类型的结果:

+---------+------+-------+
| item | COUNT(*) |
+---------+------+-------+
| group 1 | 2 |
| group 2 | 5 |
| group 3 | 8 |
+---------+------+-------+

我有了使用 NSExpression 的好主意,希望让 Core Data 为我完成所有工作。我开始转动我的轮子。 count: 表达式函数在我身上崩溃了。异常(exception)不是很清楚。使用其他表达式函数(例如 sum:)不会使应用程序崩溃。

最好将结果存储在 NSFetchedResultsController 中。我探索了其他选项,没有一个不太吸引人。在这种情况下,编写 SQL 查询并完成它比使用 Core Data 作为 SQL 包装器更有意义吗?

供您引用的源代码如下。

NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"item"];
NSExpression *totalExpression = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:keyPathExpression]];

NSExpressionDescription * totalExpressionDescription = [[NSExpressionDescription alloc] init];
[totalExpressionDescription setExpression:totalExpression];
[totalExpressionDescription setExpressionResultType:NSInteger64AttributeType];
[totalExpressionDescription setName:@"totalInstances"];

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"myEntity" inManagedObjectContext:self.managedObjectContext];
NSArray *propertiesToFetch = [[NSArray alloc] initWithObjects:strFieldName, totalExpressionDescription, nil];

[fetchRequest setEntity:entity];
[fetchRequest setReturnsDistinctResults:YES];
[fetchRequest setResultType:NSDictionaryResultType];
[fetchRequest setPropertiesToFetch:propertiesToFetch];
[fetchRequest setFetchBatchSize:20];

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:strFieldName ascending:YES];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];

NSFetchedResultsController* aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:[self managedObjectContext] sectionNameKeyPath:nil cacheName:nil];

...

NSError *error = nil;
if (![self.fetchedResultsController performFetch:&error])
{
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}

最佳答案

正如您最初提出的那样,这可以通过 NSExpression 实现,在我设计的示例中,我有一个名为“Person”的实体,它具有一个名为“emailAddress”的属性,我希望获得它的计数。

NSPropertyDescription *propDesc = [[[[model entitiesByName] objectForKey:@"Person"] propertiesByName] objectForKey:@"emailAddress"];
NSExpression *emailExpr = [NSExpression expressionForKeyPath:@"emailAddress"];
NSExpression *countExpr = [NSExpression expressionForFunction:@"count:" arguments:[NSArray arrayWithObject:emailExpr]];
NSExpressionDescription *exprDesc = [[NSExpressionDescription alloc] init];
[exprDesc setExpression:countExpr];
[exprDesc setExpressionResultType:NSInteger64AttributeType];
[exprDesc setName:@"count"];

NSFetchRequest *fr = [NSFetchRequest fetchRequestWithEntityName:@"Person"];
[fr setPropertiesToGroupBy:[NSArray arrayWithObject:propDesc]];
[fr setPropertiesToFetch:[NSArray arrayWithObjects:propDesc, exprDesc, nil]];
[fr setResultType:NSDictionaryResultType];
NSArray *results = [moc executeFetchRequest:fr error:&error];

我写了一段代码用 1000 条记录预填充数据库:

NSArray *emailAddresses = [NSArray arrayWithObjects:@"test@test.com", @"1@1.com", @"1@2.com", @"roam@test.com", @"foo@foo.com", nil];
for (int i = 0; i < 1000; i++) {
NSManagedObject *person = [NSEntityDescription insertNewObjectForEntityForName:@"Person" inManagedObjectContext:moc];
[person setValue:[NSNumber numberWithInt:i] forKey:@"aNumber"];
[person setValue:[[NSUUID UUID] UUIDString] forKey:@"aUUID"];
[person setValue:[emailAddresses objectAtIndex:(i % [emailAddresses count])] forKey:@"emailAddress"];
}

上面的代码将每个电子邮件地址插入 200 次,结果如下:

2012-05-31 15:17:42.160 Scratch[16084:10d03] CoreData: sql: SELECT t0.ZEMAILADDRESS, COUNT( t0.ZEMAILADDRESS) FROM ZPERSON t0 GROUP BY  t0.ZEMAILADDRESS 
2012-05-31 15:17:42.162 Scratch[16084:10d03] CoreData: annotation: sql connection fetch time: 0.0024s
2012-05-31 15:17:42.163 Scratch[16084:10d03] CoreData: annotation: total fetch execution time: 0.0029s for 5 rows.
(gdb) po results
(NSArray *) $2 = 0x0f811280 <_PFArray 0xf811280>(
{
count = 200;
emailAddress = "1@1.com";
},
{
count = 200;
emailAddress = "1@2.com";
},
{
count = 200;
emailAddress = "foo@foo.com";
},
{
count = 200;
emailAddress = "roam@test.com";
},
{
count = 200;
emailAddress = "test@test.com";
}
)

关于iphone - 通过核心数据的不同计数,NSExpression 到 NSFetchedResultsController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9157436/

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