Realm-java: Issue with addChangeListener on async query, without objects created.

Created on 20 Mar 2017  路  11Comments  路  Source: realm/realm-java

Goal

In Realm 3.0.0, the following code, documented in isLoaded() method, doesn't seem to work, if there aren't any Person objects created in the database.

Person person = realm.where(Person.class).findFirstAsync();
person.isLoaded(); // == false
person.addChangeListener(new RealmChangeListener() {
    @Override
    public void onChange(Person person) {
        person.isLoaded(); // Always true here
        if (person.isValid()) {
            // It is safe to access the person.
        }
    }
});

Expected Results

onChange() should be called with person.isLoaded() == true and person.isValid() == false.

Actual Results

onChange() is never called.

Version of Realm and tooling

Realm version: 3.0.0

T-Bug

Most helpful comment

Fix is merged and will be available in 3.0.1+

All 11 comments

Are you sure you don't just have to keep Person person; as a field reference of the class?

I use that workflow with more complex queries, to create objects on demand when onChange is called with object.isValid() == false.

However, since 3.0.0, onChange stopped being called at all.

Then, I simplified the code down to the use-case above, and it still isn't called.

Please try if you have it as field reference; if it is still not called then that's definitely something to look at.

Even with Person person; as field reference, onChange() isn't called.

I forgot to mention that I'm using Kotlin, but I don't see anything wrong with the decompiled code (and I don't have any other issues).

That would mean ObjectStore does not notify pending queries that don't have objects actually found to them...?

@beeender or @cmelchior might know, but currently this would mean this is a regression of https://github.com/realm/realm-java/issues/2200 with https://github.com/realm/realm-java/pull/2240 https://github.com/realm/realm-java/pull/2218 https://github.com/realm/realm-java/pull/2217

@jpmcosta if the db doesn't contain any Person, findFirstAsync will keep running until it can find one, the listener will be fired at that time. Is that your case? Does it return a valid Person if you change it to findFirst?

findFirstAsync will keep running until it can find one

That's not good for my use case, as I'm waiting for the query to end to check if there is a valid instance of Person, or if I need to create one, as described here: https://realm.io/docs/java/latest/api/io/realm/RealmQuery.html#findFirstAsync--

findFirst will not return any valid Person, as the database is (mostly) empty.

hmm, the doc doesn't match the behavior even before 3.0.0 ...
I think it should be fixed like what the doc says. @realm/java

@jpmcosta The workaround for you now is either you can use the sync version findFirst or use the findAllAsync() and check the results size in the listener block.

Either one is probably fine in my use case. Thanks! 馃憤

Fix is merged and will be available in 3.0.1+

Was this page helpful?
0 / 5 - 0 ratings

Related issues

tloshi picture tloshi  路  3Comments

Merlin1993 picture Merlin1993  路  3Comments

bryanspano picture bryanspano  路  3Comments

AlbertVilaCalvo picture AlbertVilaCalvo  路  3Comments

jjorian picture jjorian  路  3Comments