2016-02-12 17 views
6

Attualmente anche se lo faccio orderByAscending non lo fa mai in ordine ascendente. Qual è il problema che non vedo? Sto usando ParseCome interrogare in Crescente usando Parse

PFQuery *foodList = [PFQuery queryWithClassName:@"Food"]; 

[foodList whereKey:@"date" greaterThanOrEqualTo:minimumDate]; 
[foodList whereKey:@"date" lessThan:maximumDate]; 
[foodList orderByAscending:@"expiration_date"]; 


[foodList findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 
    if (!error) { 

    }]; 

Esempio

food_name  expiration_date 
Apple   1/2/15 
Banana   1/2/15 
Pear   1/3/15 
Kiwi   1/1/15 

uscita

Le uscite sarebbero molto casuale. Suppongo che l'elenco non venga ordinato mentre sta interrogando. Non sono sicuro di come risolvere questo problema.

+2

Puoi uscita il risultato che si vede? –

+0

@AndriyGordiychuk L'output è in ordine casuale. – user3281743

+0

Il codice che hai inserito qui funziona? Penso che la sintassi orderByAscending manchi la @ davanti alla stringa. Dovrebbe essere [foodList orderByAscending: @ "expiration_date"]; – Rufus

risposta

1

Ho scoperto che la Parse Query è abbastanza inaffidabile quando si inizia ad aggiungere più filtri, e potrebbe trattarsi di un bug, ma non sembra esserci molte ragioni per farlo. Quello che ho fatto, quando ho più filtri, è un NSPredicate.

NSPredicate *minPredicate = [NSPredicate predicateWithFormat:@"date >= %@", minimumDate]; 
NSPredicate *maxPredicate = [NSPredicate predicateWithFormat:@"date < %@", maximumDate]; 
NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:@[minPredicate,maxPredicate]]; 
PFQuery *foodList = [PFQuery queryWithClassName:@"Food" predicate:predicate]; 
[foodList orderByAscending:@"expiration_date"]; 

[foodList findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) { 

}]; 

Un'altra possibilità è che la query è il filtraggio per la data, e poi si ordina per "expiration_date" e non sono sicuro se c'è uno scollamento tra i due. Assicurati che la nomenclatura dell'oggetto sia ciò che desideri.

3

Io uso la variante NSSortDescriptor per eseguire l'ordinamento con l'SDK Parse e non ho avuto alcun problema con questo (anche il filtraggio su più chiavi come se fosse giusto).

Questo è come si dovrebbe ordinare utilizzando un descrittore in contrapposizione ad un tasto:

PFQuery *foodList = [PFQuery queryWithClassName:@"Food"]; 

[foodList whereKey:@"date" greaterThanOrEqualTo:minimumDate]; 
[foodList whereKey:@"date" lessThan:maximumDate]; 

NSSortDescriptor *orderBy = [NSSortDescriptor sortDescriptorWithKey:@"expiration_date" ascending:YES]; 
[foodList orderBySortDescriptor:orderBy]; 
Problemi correlati