question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Generate many conditions "equalTo" on "fly"

See original GitHub issue

Realm 4.1.

public static Merchant getMerchant(int merchantId) {
        Realm realm = Realm.getDefaultInstance();
        try {
            return realm.where(Merchant.class)
                    .equalTo(Merchant.ID, merchantId)
                    .findFirst();
        } finally {
            realm.close();
        }
}

OK. It’s work fine. But suppose I has map of fields to filter. And I want to iterate all fileds from map “fieldsAndValues” and on “fly” generate many conditions “equalTo” equals to map’s size. Something like this (psedo code):

public static Merchant getMerchant(Map<String, String> fieldsAndValues) {
        Realm realm = Realm.getDefaultInstance();
        try {
return realm.where(Merchant.class)
         .equalTo("fieldName_1", fieldValue_1)
         .equalTo("fieldName_2", fieldValue_2)
         ...
        .equalTo("fieldName_N", fieldValue_N)
         .findFirst();
        } finally {
            realm.close();
        }
}

Is it possible?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Zhuindencommented, Nov 1, 2017

I’d probably throw an IllegalArgumentException instead of return null there, but yeah 👍

It works if there is at least 1 more open Realm instance on the thread.

0reactions
alexei-28commented, Nov 1, 2017

Something like this?

public static Merchant getMerchant(Bundle filter) {
        Realm realm = Realm.getDefaultInstance();
        try {
            RealmQuery<Merchant> realmQuery = realm.where(Merchant.class);
            for (String fieldName : filter.keySet()) {
                Object fieldValue = filter.get(fieldName);
                if (fieldValue instanceof Integer) {
                    realmQuery = realmQuery.equalTo(fieldName, (int) fieldValue);
                } else if (fieldValue instanceof Boolean) {
                    realmQuery = realmQuery.equalTo(fieldName, (boolean) fieldValue);
                } else if (fieldValue instanceof String) {
                    realmQuery = realmQuery.equalTo(fieldName, (String) fieldValue);
                } else {
                    Debug.w(TAG, "getMerchant: Unsupport fieldValue's type: " + fieldValue);
                    return null;
                }
            }
            return realmQuery.findFirst();
        } finally {
            realm.close();
        }
    }

Read more comments on GitHub >

github_iconTop Results From Across the Web

PHP If Statement with Multiple Conditions - Stack Overflow
I want echo "true" if $var is equal to any of the following values abc , def , hij , klm , or...
Read more >
Using conditions for personalization - Help Center - WordFly
Conditions give you a powerful way to send customized content to each subscriber. Instead of creating a new email design with different content ......
Read more >
How your flight emits as much CO2 as many people do in a year
How your flight emits as much CO2 as many people do in a year. Even short-haul flights produce huge amounts of CO2, figures...
Read more >
Power Automate Trigger Conditions made EASY
Within this blog we'll look at how we can quickly and easily create our trigger conditions so that we know they are working....
Read more >
If Statements - Happy Coding
You can think about the code like this: “If the score is greater than or equal to 90, then display the 'Congratulations! '...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found