Я могу дать вам некоторое вдохновение из-за того, что я предполагаю, что «статус» может быть только 0 или 1. Если это не так, то - я думаю - вам нужно выполнить два разделенных fetch с разными исходными значениями (один для выборки счетчика, указывающий user1, а другой - счет состояния, когда user = user1 и status = 1).
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
//create entity
NSEntityDescription *entity = [NSEntityDescription entityForName:@"UserProfile" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
//filter for user = user1
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"user == %@",user1];
[fetchRequest setPredicate:predicate];
//count of the status == 1
NSExpressionDescription* sumStatusIsOne = [[NSExpressionDescription alloc] init];
[sumStatusIsOne setExpression:[NSExpression expressionForFunction:@"sum:"
arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"status"]]]];
[sumStatusIsOne setExpressionResultType:NSDecimalAttributeType];
[sumStatusIsOne setName:@"sumStatusIsOne"];
//count of the location
NSExpressionDescription* locationCount = [[NSExpressionDescription alloc] init];
[locationCount setExpression:[NSExpression expressionForFunction:@"count:"
arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"Location"]]]
];
[locationCount setExpressionResultType:NSDecimalAttributeType];
[locationCount setName:@"locationCount"];
NSArray *subtractComponents = [NSArray arrayWithObjects:
locationCount.expression,
sumStatusIsOne.expression, nil];
//Count of zero status !!!Important, it is working if the 'status' can be only 1 or 0!!!
NSExpression *countOfZeroStatusExpression = [NSExpression expressionForFunction:@"from:subtract:" arguments:subtractComponents];
NSExpressionDescription* countOfZeroStatus = [[NSExpressionDescription alloc] init];
[countOfZeroStatus setExpression:countOfZeroStatusExpression];
[countOfZeroStatus setExpressionResultType:NSDecimalAttributeType];
[countOfZeroStatus setName:@"countOfZeroStatusExpression"];
[fetchRequest setPropertiesToFetch:[NSArray arrayWithObjects:@"Location", locationCount,countOfZeroStatus, nil]];
[fetchRequest setPropertiesToGroupBy:[NSArray arrayWithObject:@"Location"]];
[fetchRequest setResultType:NSDictionaryResultType ];
NSError *error;
NSArray *grouppedResults = [self.managedObjectContext executeFetchRequest:fetchRequest error:&error];
NSLog(grouppedResults);
Есть ли статус или текст? Какие ценности он может принять? – pbasdf
Задайте предикаты для ваших условий (например, статус = 0 и user = user1 и все остальное). Для получения вывода сгруппированы в разные местоположения, вы можете использовать 'setPropertiesToGroupBy'. Играйте с ним немного. Также можно посмотреть 'setPropertiesToFetch'. – Zen
Состояние @pdasdf - это число. значение может быть от 0 до 5 – Rocker