6

I have a Entity called deal and deal has a property called date which is the time this deal object inserted into the store.

and one day may have several deals.

So I want count some data group by day, I want fetch dayand countofsomething
like:

2013-06-03 3
2013-06-02 4

and I don't want to use sectionPath because it only put deals into section.

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

Thanks all of you.

Puttin
  • 1,596
  • 23
  • 27
  • it seems that there is no way to do that. according to http://stackoverflow.com/questions/9359437 – Puttin Jun 05 '13 at 06:50

1 Answers1

4

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 would be something like this.

({
    count = 1;
    notes = "glenny and me";
},
{
    count = 6;
    notes = macair;
})
nhahtdh
  • 55,989
  • 15
  • 126
  • 162
Underdog
  • 785
  • 10
  • 21
  • Oh, I almost forget this question. I solved by myself some times before. But still thank you for your answer! – Puttin Sep 06 '13 at 12:47
  • @AlexanderLongbeach My working solution now is that a `NSFetchRequest` with a `NSPredicate`(date>=2013.12.17 00:00 AND date <2013.12.18 00:00), set the `ResultType` to `NSCountResultType`. Maybe not good for you. – Puttin Dec 17 '13 at 10:05