2016-02-24 11 views
5

Ho seguente codice per il recupero e il raggruppamento dei risultati di ricerca:Can NSFetchRequest propertiesToGroupBy essere case insensitive?

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]; 

che funziona molto bello, ma ho affrontato per il rilascio e il tipo di stucked con, in modo da ho bisogno di fare questo propertiesToGroupBy (tipo di proprietà è NSString *) case insensitive in qualche modo. Al momento ho output seguente:

<_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"; 
} 
) 

che non è corretto, quindi ho bisogno primi 2 articoli da essere in un'unica sezione, causa questo è lo stesso artista.

Esiste un modo per raggiungerlo?

risposta

0

provare a utilizzare 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]]; 

prega di notare che non posso controllare il codice in questo momento in XCode, quindi potrebbe contenere errori di stampa. Mi dispiace, ma penso che il punto principale sia chiaro.

+0

Sembra impossibile raggruppare per NSExpressionDescription per me. Dopo [request setPropertiesToGroupBy: myExpressions]; Ho sempre 'NSInvalidArgumentException', motivo: 'espressione di keypath non valida – iiFreeman

Problemi correlati