0

How would I accomplish a similar query in realm java like that one asked in this question: how do I query sql for a latest record date for each user Cause it seems realm lacks a group by method when fetching queries

Here's my RealmObject:

public class Memo extends RealmObject {
    @Ignore
    public static final int SENT = 1;
    @Ignore
    public static final int UNREAD = 2;
    @Ignore
    public static final int READ = 3;
    @Ignore
    public static final int PENDING = 4;
    private static final String TAG = Memo.class.getName();
    @PrimaryKey
    public String id = "";
    @Index
    public String connection = "";
    public String subject = "";
    public String body = "";
    public int status = 0;
    public Date date;
    public Boolean sentByMe = false;
    public String remoteId = "";
    public Boolean isFile = false;
    public String filepath = "";
}

This represents the user:

public String connection
Michael
  • 713
  • 10
  • 27

1 Answers1

1
RealmResults<Memo> latestMemos = realm.where(Memo.class)
                                      .findAllSorted("date", Sort.DESCENDING)
                                      .distinct("connection");
EpicPandaForce
  • 79,669
  • 27
  • 256
  • 428
  • I don't really trust `distinctAsync()` which is why I specified the full-sync version. I don't seem to find _why_ I don't trust it though... all I do see is that `distinct()` is distinct-in-place. You can try it out, but you should verify that this one works first :p I think it should – EpicPandaForce Sep 07 '16 at 21:07
  • so it's better to run it in asynctask? – Michael Sep 07 '16 at 21:12
  • Are you using [RealmRecyclerViewAdapter](https://github.com/realm/realm-android-adapters)? – EpicPandaForce Sep 07 '16 at 21:12
  • I think you can give `findAllSorted().distinctAsync()` a try. But `findAllSortedAsync().distinctAsync()` will most likely not work. I have **not** used this before though, so I'm not certain. – EpicPandaForce Sep 07 '16 at 21:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/122862/discussion-between-michael-and-epicpandaforce). – Michael Sep 07 '16 at 21:16