Realm.io request with GroupBy

I want to group multiple accounts per month, can I do this using Realm.io?

public class Account extends RealmObject { ..... private Date date; } RealmResults accounts = realm.where(Account.class) .beginGroup() .equalTo("date", "MONTH(date)")//<----- wrong code .endGroup() .findAll(); 

thanks

+5
source share
1 answer

The kingdom does not yet support GroupBy. Also keep in mind that beginGroup () actually matches parentheses. Thus, your request is actually interpreted as:

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

In Realm, you need to do something like this to select one month:

 // 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(); 

or something like this to determine when a month changes

 // 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; } 
+2
source

All Articles