Realm-java: Query with in() returns an empty RealmResults

Created on 20 May 2019  ·  17Comments  ·  Source: realm/realm-java

Goal

I want to query a table and retrieve objects by their IDS

Actual Results

The RealmResults returns an empty list, even if the objects are in the table (checked with Realm Studio).

Steps & Code to Reproduce

Requesting the table with multiples IDs always return an empty list, but querying the IDs separately returns a list with the object.

realm.where("mytable").equalTo("uid", "firstUID").findAll()-> returns a list with the object
realm.where("mytable").equalTo("uid", "secondUID").findAll()-> returns a list with the object

realm.where("mytable").beginGroup().equalTo("uid", "firstUID").or().equalTo("uid", "secondUID").endGroup().findAll()-> returns an empty list

realm.where("mytable").in("uid", new String[]{"firstUID", "secondUID"}).findAll()-> returns an empty list

Maybe it has something to do with the following PR: (Realm Core PR #3250).
Reverting to 5.9.1 solved the issue.

Version of Realm and tooling

Realm version(s): ? 5.11.0

Realm Sync feature enabled: No

Android Studio version: ? 3.4 stable

Android Build Tools version: ? 28.0.3

Gradle version: ? 5.1.1

Which Android version and device(s): ? Android API 28, Emulator

O-Community T-Bug

Most helpful comment

Fixed in 5.13.1

All 17 comments

Are we talking about https://github.com/realm/realm-core/pull/3250?

realm.where("mytable").beginGroup().equalTo("uid", "firstUID").or().equalTo("uid", "secondUID").endGroup().findAll()-> returns an empty list

This is supposed to work. 😞

Although it says:

This change does not try to optimise indexed columns which should be running O(log(N)*C). The benchmark with indexes turned on runs in 3.5 seconds.

If the uid column is Primary Key, then it is Indexed, and therefore should have been unaffected by this change.


I was wondering if https://github.com/realm/realm-core/pull/3271 is related, but it is for int columns...

I should have mentioned my "uid" field is of type String, and not the Primary key in this given table

It is also not marked with @Index, correct?

Correct

@cmelchior

@mick1418 Could you please make a minimal self-contained repro case? Or a PR with a test-case that shows this?
(We do have extensive query tests here: https://github.com/realm/realm-java/blob/master/realm/realm-library/src/androidTest/java/io/realm/RealmQueryTests.java)

Getting this issue as well. In my case the field is marked with @Index but I am only getting 1 or no data back instead of around 20 items. Realm version 5.10.0 works fine still.

Haven't been able to determine a use-case to make available.
Can state though that the query has a equalsTo and a in which is likely the cause

I cannot reproduce this with a unit test:

    @Test
    public void in_primaryKey() {
        realm.executeTransaction(r -> {
            for (int i = 0; i < 5; i++) {
                realm.createObject(AllJavaTypes.class, i);
            }
        });

        RealmResults<AllJavaTypes> results = realm.where(AllJavaTypes.class).in(AllJavaTypes.FIELD_ID, new Long[]{2L, 3L, 4L}).findAll();
        assertEquals(3, results.size());

        realm.executeTransaction(r -> {
            for (int i = 0; i < 5; i++) {
                realm.createObject(PrimaryKeyAsString.class, Integer.toString(i));
            }
        });

        RealmResults<PrimaryKeyAsString> stringResults = realm.where(PrimaryKeyAsString.class).in(PrimaryKeyAsString.FIELD_PRIMARY_KEY, new String[]{"2", "3", "4"}).findAll();
        assertEquals(3, stringResults.size());
    }

For neither integer nor String primary key

I have near same problem. Where query return zero values, but simply loop show that values are here.
This is not 100% reproducible. Happens randomly for users (until they not clear app data). But probably happens after calling: `report.reportValues.deleteAllFromRealm();' on another 'report' entity which not linked with problematic entity. ReportValues also unique for each 'report' entity.

Realm version(s): 5.11.0, 5.12.0
Downgrade to 5.10.0 solved problem.

RealmResults<ReportValue> dbrvs = report.getReportValues().where().equalTo("category", TaskCategory.BEFORE_REPORT.ordinal()).findAll();
Log.e(LOG_TAG, "ReportValues: " + dbrvs.size());
for (ReportValue rv : report.getReportValues()) {
    if (rv.getCategory() == TaskCategory.BEFORE_REPORT.ordinal()) {
        Log.e(LOG_TAG,  rv.toString());
    }
 }

Result:

E/ReportHubFragment: ReportValues: 0   **(but should be 1)**
E/ReportHubFragment: ReportValue{attachement=null, id=-1711229038, subTaskId=null, name='null', description='null', priority='null', clientId='null', value='null', oldValue='null', hotline='null', oldHotline='null', posSpecific=true, requiresPhoto=false, completed=true, max=0, min=0, type=4, category=1, sku=null}
public class Report extends RealmObject {
    @PrimaryKey
    private String guid;
....
    private RealmList<ReportValue> reportValues;
}
public class ReportValue extends RealmObject {
    @Index
    private Long id;
........
    @Index
    private int category;

@cmelchior
I created test project which can reproduce bug https://github.com/Quarx2k/Realm-issue-6522
5.11/5.12 Realm

2019-06-21 20:27:50.686 24924-24924/? E/MainActivity: Report 97 Values Size: 5 
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: Report 97 Values Query Size: 1 
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: The bug ;(
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: Report 98 Values Size: 5 
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: Report 98 Values Query Size: 1 
2019-06-21 20:27:50.687 24924-24924/? E/MainActivity: The bug ;(
2019-06-21 20:27:50.688 24924-24924/? E/MainActivity: Report 99 Values Size: 5 
2019-06-21 20:27:50.688 24924-24924/? E/MainActivity: Report 99 Values Query Size: 1 
2019-06-21 20:27:50.688 24924-24924/? E/MainActivity: The bug ;(
2019-06-21 20:27:50.689 24924-24924/? E/MainActivity: Report 100 Values Size: 5 
2019-06-21 20:27:50.689 24924-24924/? E/MainActivity: Report 100 Values Query Size: 5 

5.10 Realm

2019-06-21 20:31:34.112 25324-25324/? E/MainActivity: Report 97 Values Size: 5 
2019-06-21 20:31:34.112 25324-25324/? E/MainActivity: Report 97 Values Query Size: 5 
2019-06-21 20:31:34.113 25324-25324/? E/MainActivity: Report 98 Values Size: 5 
2019-06-21 20:31:34.113 25324-25324/? E/MainActivity: Report 98 Values Query Size: 5 
2019-06-21 20:31:34.113 25324-25324/? E/MainActivity: Report 99 Values Size: 5 
2019-06-21 20:31:34.113 25324-25324/? E/MainActivity: Report 99 Values Query Size: 5 
2019-06-21 20:31:34.114 25324-25324/? E/MainActivity: Report 100 Values Size: 5 
2019-06-21 20:31:34.114 25324-25324/? E/MainActivity: Report 100 Values Query Size: 5 

Hope it helps :)

@Quarx2k Thank you very much for this 👏 . I can reproduce the behavior and is looking into it.

It seems to be a bug with Indexes. The error goes away if you remove Indexes.

(you can't remove the index of a primary key field though)

Realm Core 5.23.1 release should have the fix, now Realm-Java needs to update the Core version in next release 🤔

Fixed in 5.13.1

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cmelchior picture cmelchior  ·  3Comments

pawlo2102 picture pawlo2102  ·  3Comments

nolanamy picture nolanamy  ·  3Comments

yuwu picture yuwu  ·  3Comments

jjorian picture jjorian  ·  3Comments