Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue with addChangeListener on async query, without objects created. #4352

Closed
jpmcosta opened this issue Mar 20, 2017 · 11 comments
Closed

Issue with addChangeListener on async query, without objects created. #4352

jpmcosta opened this issue Mar 20, 2017 · 11 comments
Assignees
Labels

Comments

@jpmcosta
Copy link

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

@jpmcosta jpmcosta changed the title Issue with async addChangeListener without objects created. Issue with addChangeListener on async query, without objects created. Mar 20, 2017
@Zhuinden
Copy link
Contributor

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

@jpmcosta
Copy link
Author

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.

@Zhuinden
Copy link
Contributor

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

@jpmcosta
Copy link
Author

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).

@Zhuinden
Copy link
Contributor

Zhuinden commented Mar 20, 2017

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 #2200 with #2240 #2218 #2217

@beeender
Copy link
Contributor

@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?

@jpmcosta
Copy link
Author

jpmcosta commented Mar 21, 2017

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.

@beeender
Copy link
Contributor

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

@beeender
Copy link
Contributor

beeender commented Mar 21, 2017

@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.

@jpmcosta
Copy link
Author

jpmcosta commented Mar 21, 2017

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

beeender added a commit that referenced this issue Mar 21, 2017
Fix #4352
The findFirstAsync()'s behavior doesn't match the javadoc from the first
day the API was introduced. It kept running the query until it could
find a row match the query condition. This behavior create difficulties
if user want to check if there is no object in the db. Also it was not
consistent with the behavior of findFirst() which will return an invalid
row in the same condition.
@beeender beeender self-assigned this Mar 21, 2017
@Zhuinden
Copy link
Contributor

Zhuinden commented Mar 21, 2017

Fix is merged and will be available in 3.0.1+

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 16, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

4 participants