Realm-java: RealmResults.sort() on asynchronous query removes all conditions.

Created on 20 Jan 2017  ·  7Comments  ·  Source: realm/realm-java

Goal

Sort RealmResults.

Expected Results

Either sorted results or an exception. I know that I should have done findAllSortedAsync, however, after changing synchronous queries to asynchronous ones, this behaviour snuck up on me. I think there should be some kind of warning, right?

Actual Results

All objects of the given RealmModel, ignoring the conditions on the query behind the RealmResult.

Code Sample

// Query that returns a few objects
results = query.findAllAsync();

// 0
Timber.w("Query returning %d results before sort.", results.size());

results = results.sort(new String[]{"fieldA", "fieldB", "fieldC"},
new Sort[]{Sort.DESCENDING, Sort.ASCENDING, Sort.ASCENDING});

// All objects
Timber.w("Query returning %d results after sort.", results.size());

Version of Realm and tooling

2.3.0

Realm sync feature enabled: no

Android Studio version: 2.2.3

Which Android version and device: Emulator API 25.

T-Bug

Most helpful comment

IMO, this is a bug. And it has been mentioned here https://github.com/realm/realm-java/issues/3668#issuecomment-255378795 before.

Anyway, this will be fixed by #3834

All 7 comments

This is working as intended. RealmResults are not yet loaded, so it behaves as an empty list until it is. You can check this by doing results.isLoaded(). If you register a changelistener you will be notified when it is loaded. Note we also have a findAllSortedAsync() that might suit your more?

IMO, this is a bug. And it has been mentioned here https://github.com/realm/realm-java/issues/3668#issuecomment-255378795 before.

Anyway, this will be fixed by #3834

@benj56 The current workaround is just use findAllSortedAsync() instead.

@cmelchior Thanks, findAllSortedAsync is indeed best suited for my needs. However, I would not expect a change in size of the Results due to sorting under any circumstances.

What I would expect is the sort perhaps not being applied to the data since it is not yet loaded, but I would not expect it to cancel the loading of data and instead fetch all objects, dropping any conditions. What if there was data in the Realm that was never supposed to be displayed?

@cmelchior I do think it's a bit of a bug that you are allowed to call sort() on a class that is not yet loaded, considering as you can see, it doesn't really make sense that you can do that and indicates user error, and should fail early (in my opinion)

Unloaded RealmResults were specifically designed to work as an empty List, and calling sort on that is possible. The argument at the time was that it should be possible for UI code to not care about the state of the RealmResult. All it should care about is just render whatever it was given. From that perspective, it is working as intended.

That isn't to say that we cannot revisit that argument, but it opens up a whole (bigger) discussion.

Edit: That said. My mind was a bit foggy when I read the OP first time, and I agree with @beeender that it is a bug. The correct behaviour right now would be that results should continue to be 0.

3834 is merged to master. will be fixed in v2.4.0.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Merlin1993 picture Merlin1993  ·  3Comments

nolanamy picture nolanamy  ·  3Comments

AlbertVilaCalvo picture AlbertVilaCalvo  ·  3Comments

yuwu picture yuwu  ·  3Comments

Frasprite picture Frasprite  ·  3Comments