2015-05-25 8 views
5

Ich möchte einige Konten nach Monat gruppieren, kann ich dies mit Realm.io tun?Realm.io Abfrage mit GroupBy

public class Account extends RealmObject { 
..... 
private Date date; 
} 

RealmResults accounts = realm.where(Account.class) 
.beginGroup() 
.equalTo("date", "MONTH(date)")//<----- wrong code 
.endGroup() 
.findAll(); 

dank

Antwort

2

Realm unterstützen GroupBy noch nicht. Beachten Sie auch, dass beginGroup() tatsächlich dasselbe ist wie Klammern. So Ihre Abfrage ist eigentlich interpretiert als:

// SQL pseudo code 
SELECT * FROM Account WHERE (date = MONTH(date)) 

In Realm Sie so etwas wie dies würde zu tun haben, einen einzigen Monat zu wählen:

// Between is [ monthStart, monthEnd ] 
Date monthStart = new GregorianCalendar(2015, 5, 1).getTime(); 
Date monthEnd = new GregorianCalendar(2015, 6, 1).getTime() - 1; 
accounts = realm.where(Account.class).between("date", monthStart, monthEnd).findAll(); 

oder so etwas zu erkennen, wenn ein Monat ändert

// pseudo code. You might want to use Calendar instead 
accounts = realm.where(Account.class).findAllSorted("date") 
Iterator<Account> it = accounts.iterator(); 
int previousMonth = it.next().getDate().getMonth(); 
while (it.hasNext) { 
    int month = it.next().getDate().getMonth(); 
    if (month != previousMonth) { 
    // month changed 
    } 
    previousMonth = month; 
} 
+0

Vielen Dank für Ihre schnelle Antwort. zuerst funktioniert für mich. – schwertfisch