如何按日分组核心数据? [英] how to group by day with core data?
问题描述
我有一个名为deal
的实体,并且deal
有一个名为date
的属性,这是此交易对象插入商店的时间.
I have a Entity called deal
and deal
has a property called date
which is the time this deal object inserted into the store.
和一天可能有几个deal
.
所以我想按天计算一些数据组,我想获取day
和countofsomething
像:
So I want count some data group by day, I want fetch day
and countofsomething
like:
2013-06-03 3
2013-06-02 4
,并且我不想使用sectionPath
,因为它只会将交易放入分区.
and I don't want to use sectionPath
because it only put deals into section.
我知道我可以通过具有另一个属性(类型:字符串)(例如dayOfTheDate
)来实现此目的,该属性类似于每个对象中的2013-06-03
.
I know I can have this done by have another property(type:string) like dayOfTheDate
which is like 2013-06-03
in each object.
顺便说一句,瞬态属性在这种情况下似乎不起作用
btw, transient property don't seem to work in this situation
您能明白我在找什么吗?
Could you understand what I am looking for?
在这里评论,以便我提供更多详细信息
Comment here so I can provide more detail
谢谢大家.
推荐答案
计算相同笔记数时的操作方式示例
Sample of how I did it when I counted number of same notes
NSEntityDescription* entity = [NSEntityDescription entityForName:@"Assets"
inManagedObjectContext:[appDelegate managedObjectContext]];
NSAttributeDescription* statusDesc = [entity.attributesByName objectForKey:@"notes"];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath: @"assetUrl"]; // Does not really matter
NSExpression *countExpression = [NSExpression expressionForFunction: @"count:"
arguments: [NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: @"count"];
[expressionDescription setExpression: countExpression];
[expressionDescription setExpressionResultType: NSInteger32AttributeType];
[searchFetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:statusDesc,expressionDescription, nil]];
[searchFetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:statusDesc]];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"timestamp" ascending:NO];
[searchFetchRequest setSortDescriptors:@[sortDescriptor]];
[searchFetchRequest setFetchLimit:10];
NSPredicate *query = [NSPredicate predicateWithFormat:@"notes contains[cd] %@",_txtCameraNote.text];
[searchFetchRequest setPredicate:query];
[searchFetchRequest setResultType:NSDictionaryResultType];
NSArray *fetchedObjects = [appContext executeFetchRequest:searchFetchRequest error:nil];
fetchedObjects
就是这样.
({
count = 1;
notes = "glenny and me";
},
{
count = 6;
notes = macair;
})
这篇关于如何按日分组核心数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!