Can NSFetchRequest propertiesToGroupBy be case insensitive?

I have the following code to fetch and group search results:

NSFetchRequest *request = [[NSFetchRequest alloc] initWithEntityName:@"Song"]; [request setPredicate:[NSCompoundPredicate andPredicateWithSubpredicates:predicates]]; [request setResultType:NSDictionaryResultType]; [request setSortDescriptors: @[[NSSortDescriptor sortDescriptorWithKey:@"timestamp" ascending:YES]]]; NSExpression *countExpression = [NSExpression expressionWithFormat:@"count:(SELF)"]; NSExpressionDescription *expressionDescriprion = [[NSExpressionDescription alloc] init]; [expressionDescriprion setName:@"count"]; [expressionDescriprion setExpression:countExpression]; [expressionDescriprion setExpressionResultType:NSInteger64AttributeType]; NSArray *properties = @[@"artistName", @"songName"]; [request setPropertiesToFetch:[properties arrayByAddingObject:expressionDescriprion]]; [request setPropertiesToGroupBy:properties]; self.fetchedItems = [self.managedObjectContext executeFetchRequest:request error:nil]; 

which works very well, but I ran into a problem and the view got stuck, so I need to make this ToGroupBy property (property type NSString *) case-insensitive somehow . At the moment , I have the following output :

 <_PFArray 0x7fa3e3ed0d90>( { artistName = "System of a Down"; count = 44; songName = "Lonely Day"; }, { artistName = "System Of A Down"; count = 2; songName = "Lonely Day"; }, { artistName = "System of a Down"; count = 4; songName = "Chop Suey"; } ) 

which is wrong, so I need the first 2 elements to be in the same section, because this is the same artist.

Is there any way to achieve this?

+5
source share
1 answer

Try using NSExpressionDescription:

 NSExpression *artistKeyPathExpression = [NSExpression expressionForKeyPath:@"artistName"]; NSExpression *artistExpression = [NSExpression expressionForFunction:@"uppercase:" arguments:@[artistKeyPathExpression]]; NSExpressionDescription *artistExpressionDescription = [NSExpressionDescription new]; artistExpressionDescription.name = @"groupByArtist"; artistExpressionDescription.expression = artistExpression; artistExpressionDescription.expressionResultType = NSStringAttributeType; NSExpression *songKeyPathExpression = [NSExpression expressionForKeyPath:@"songName"]; NSExpression *songExpression = [NSExpression expressionForFunction:@"uppercase:" arguments:@[songKeyPathExpression]]; NSExpressionDescription *songExpressionDescription = [NSExpressionDescription new]; songExpressionDescription.name = @"groupBySongName"; songExpressionDescription.expression = songExpression; songExpressionDescription.expressionResultType = NSStringAttributeType; [request setPropertiesToGroupBy:@[artistExpressionDescription, songExpressionDescription]]; 

Please note that I cannot verify this code right now in Xcode, so it may contain typos. Sorry about that, but I think the point is clear.

0
source

Source: https://habr.com/ru/post/1243777/


All Articles