realm-java icon indicating copy to clipboard operation
realm-java copied to clipboard

Infinite loop when querying an empty table

Open cbedoya opened this issue 10 years ago • 9 comments

Hello

I am trying to perform a query which basically will delete all the objects that don't match a list of ids.

RealmQuery<Hit> query = realm.where(MyObject.class);
for (int i = 0; i < objectList.size(); i++) {
    MyObject obj = objectList.get(i);
    query.notEqualTo("id", obj.getId());
}

query.findAll().clear();

This works perfectly when the database already has objects, but for example the first run it obviously will not have any objects, and I don't think adding a flag or validate that the database has objects is a good approach.

*Edit * This happens also when the stored objects matches all the ids, for example if I have stored objects with ids [1, 2, 3] and I try to query all the objects that don't have the ids [1, 2, 3].

Thanks!

cbedoya avatar Mar 09 '16 06:03 cbedoya

Hi, why it is an infinite loop? Can you please format the code to make the braces {} match?

beeender avatar Mar 09 '16 07:03 beeender

Hi thanks for answering, I removed the extra brace, so not sure if its an infinite loop, but it gets stuck when calling query.findAll(). BTW this happens on iOS as well, I've tried with Obj-C, not sure if I need to submit a ticket for ObjC as well.

Thanks

cbedoya avatar Mar 09 '16 17:03 cbedoya

I think i know what the problem is. When you query.notEqualTo(), it will chain the queries before together. If you do it in the loop, the query will be chained with thousands of conditions which will be slow if there are too many.

You can try something below:

for (int i = 0; i < objectList.size(); i++) {
    RealmQuery<Hit> query = realm.where(MyObject.class);
    //MyObject obj = new MyObject(); // I assume this is not correct
    MyObj obj = objectList.get(i);
    query.notEqualTo("id", obj.getId());
    query.findAll().clear();
}

Depends on what do you want to do, and how did you get the objectList, I believe there are better ways than above.

beeender avatar Mar 10 '16 01:03 beeender

Ok, I understand that it will chain the condition, which is what I want. The object list comes from a webservice, this list changes constantly, new objects are added, but also there are some situations when the objects can be deleted, so everytime I get the list it needs to match with the realm list. For example:

Realm Object ids:
[1, 2, 3]

Web Service List(objectList) ids:
[1, 3, 4]

Query:
id != 1 && id != 3 && id != 4

Delete matches

Object with id 2 gets deleted on realm and object with id 4 is added
[1, 3, 4]

I know this works when there is at least one different object between the realm objects and the web service list, but when there are no matches or when the table is empty, the query hangs and it never returns a result.

Also I can just delete the entire objects before inserting the new ones but I don't think this is an optimal solution, or is there any other way to achieve this?

Thanks!

cbedoya avatar Mar 10 '16 05:03 cbedoya

Hi @cbedoya

I just tried with the following unit test:

    @Test
    public void notEqualTo_noData() {
        assertEquals(0, realm.where(AllTypes.class).count());
        RealmResults<AllTypes> results = realm.where(AllTypes.class).notEqualTo(AllTypes.FIELD_LONG, 1).findAll();
        assertEquals(0, results.size());
    }

And it doesn't seem to hang. Can you create a small sample project that exposes the behaviour you are seeing. That way it will be easier for us to debug.

cmelchior avatar Mar 10 '16 06:03 cmelchior

Hi @cmelchior

Sure, I will create one that simulates everything and see if the issue is present.

cbedoya avatar Mar 10 '16 07:03 cbedoya

I used to use some tricks like below:

RealmResults<MyObject.class> results = realm.where(MyObject.class).findAll();
for (MyObject obj : results) {
    obj.setValid(false);
}

// You may also just put code inside this loop to where the objectList was generated.
for (int i = 0; i < objectList.size(); i++) {
    //MyObject obj = new MyObject(); // I assume this is not correct
    MyObj obj = objectList.get(i);
    obj.setValid(true);
    obj = realm.copyToRealmOrUpdate(obj);
}
realm.where(MyObject.class).equalTo("valid", false).findAll().clear();

beeender avatar Mar 10 '16 07:03 beeender

@cmelchior I've tried with a new project and I could not replicate the issue, so I guess something else is messing around with the realm. I think this issue can be closed now

@beeender I think this is a good approach, I was thinking something similar with a last update date. I will try your suggestion

Thanks for your help

cbedoya avatar Mar 10 '16 18:03 cbedoya

@cbedoya I am closing this. Let us know if we can help on others :)

beeender avatar Mar 11 '16 01:03 beeender