realm-java: Iterating result very slow

Hi,

Here are my java classes :

public class PublicArticleEntity extends RealmObject {

    /**
     * For fast import no @PrimaryKey defined
     */
    private String id;

    private String type;

    @Index
    private String manufacturerId;

    private Boolean sterilizable;

    private Boolean active;

    private Date importModified;

    private RealmList<PublicArticlePartNumberEntity> partNumbers = new RealmList<>();
    private RealmList<PublicArticleCodificationEntity> codifications = new RealmList<>();

public class PublicArticlePartNumberEntity extends RealmObject {

    @Index
    private String partNumber;

    @Index
    private String normalizedPartNumber;

    private PublicArticleEntity publicArticle;
}

public class PublicArticleCodificationEntity extends RealmObject {

    @Index
    private String type;

    @Index
    private String value;

    private PublicArticleEntity publicArticle;
}

And here is the piece of code where I have a problem. The query returns 24 results and I use the limit to get the 21 first elements. It tooks 2000ms to do the iteration 😮

RealmResults<PublicArticlePartNumberEntity> entities = realm.where(PublicArticlePartNumberEntity.class)
                .equalTo("publicArticle.manufacturerId", manufacturerId.toString())
                .beginGroup()
                .contains("partNumber", search, Case.INSENSITIVE)
                .or()
                .contains("normalizedPartNumber", search, Case.INSENSITIVE)
                .endGroup()
                .findAllSorted("partNumber", Sort.ASCENDING);

        Set<String> items = new HashSet<>();
        int listSize = entities.size();
        for (int i = 0; i < listSize && i < limit; i++) {
            items.add(entities.get(i).getPartNumber());
        }

Here the state of each : screenshot at oct 06 15-04-13

Because Realm is using lazy loading the request if very very fast, but the for is taking 2s. Did I miss something ?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 31 (25 by maintainers)

Commits related to this issue

Most upvoted comments

So here is the test as @beeender requested. I’m in debug mode so it takes more time and on a production device that is waaaaaay slower than my One Plus 3T, but it’s a good thing to see differences.

First test with current code (not changed) : first

Test with code

entities.get(0).getPartNumber();
int listSize = entities.size();
screenshot at oct 11 12-38-29