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

Result of QueryBuilder.build().count() dosen't match QueryBuilder.build().count().size() #771

Closed
fakerqu opened this issue Sep 27, 2019 · 2 comments

Comments

@fakerqu
Copy link

commented Sep 27, 2019

#764 Issue Basics

  • ObjectBox version (are using the latest version?): 2.3.4
  • Reproducibility: [always]

Reproducing the bug

Description

Result of QueryBuilder.build().count() dosen't match QueryBuilder.build().count().size()

Code

List<MtLogEntity> entities = ObjectBoxManager.getInstance().getMtLogEntityBox().query()
                    .filter(entity -> logFile.getName().contains(entity.getFirstLogName()))
                    .build().find();
            long count = ObjectBoxManager.getInstance().getMtLogEntityBox().query()
                    .filter(entity -> logFile.getName().contains(entity.getFirstLogName()))
                    .build().count();

Logs & stackstraces

While I was debugging the result of build().count()==1 ,but the size of entities is 0!
bug
My database is quite simple and only contains one entity
捕获

@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

commented Sep 30, 2019

Thanks for reporting. query.count() does not use a given filter. So the result size will be different.

Filters only work with find() as stated in the documentation.

/**
* Sets a filter that executes on primary query results (returned from the db core) on a Java level.
* For efficiency reasons, you should always prefer primary criteria like {@link #equal(Property, String)} if
* possible.
* A filter requires to instantiate full Java objects beforehand, which is less efficient.
* <p>
* The upside of filters is that they allow any complex operation including traversing object graphs,
* and that filtering is executed along with the query (preferably in a background thread).
* Use filtering wisely ;-).
* <p>
* Also note, that a filter may only be used along with {@link Query#find()} and
* {@link Query#forEach(QueryConsumer)} at this point.
* Other find methods will throw a exception and aggregate functions will silently ignore the filter.
*/
public QueryBuilder<T> filter(QueryFilter<T> filter) {

So this is a documentation oversight, but we might as well throw an error as the other Query methods do.

@greenrobot-team greenrobot-team self-assigned this Sep 30, 2019
@greenrobot-team greenrobot-team added this to the 2.4.0 milestone Oct 7, 2019
@greenrobot-team

This comment has been minimized.

Copy link
Collaborator

commented Oct 7, 2019

Starting with 2.4.0-RC calling count() or remove() while using a filter throws.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.