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.
}
}
});
onChange() should be called with person.isLoaded() == true and person.isValid() == false.
onChange() is never called.
Realm version: 3.0.0
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?
findFirstAsyncwill 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+
Most helpful comment
Fix is merged and will be available in 3.0.1+