how to group by day with core data?

后端 未结 1 587
日久生厌
日久生厌 2021-01-06 03:10

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

an

相关标签:
1条回答
  • 2021-01-06 03:59

    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;
    })
    
    0 讨论(0)
提交回复
热议问题