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

Realm Query for List #4990

Closed
pollux- opened this issue Jul 19, 2017 · 4 comments
Closed

Realm Query for List #4990

pollux- opened this issue Jul 19, 2017 · 4 comments
Labels

Comments

@pollux-
Copy link

pollux- commented Jul 19, 2017

I have a product Model Like this

public class Product
{
private String title;
private RealmList<Meta> meta;

public class Meta {
public String key;
public String value;
     }
}

Model is populated from below JSON.

[{
	"title": "BBQ chicken",
	"meta": [{
			"key": "content",
			"value": "Chicken"

		},
		{
			"key": "protein",
			"value": "Chicken"
		}
	]

}, {
	"title": "Beef BBQ",
	"meta": [{
			"key": "content",
			"value": "Beef"

		},
		{
			"key": "protein",
			"value": "good"
		}, {
			"key": "ingredient",
			"value": "chicken"
		}
	]

}]

What I need is, filter all products which have content="chicken". So I ended up in writing a query like this.

realm.where(Product.class).equalTo("meta.key","content").equalTo("meta.value","chicken").findAll().
But I ended up in result set with both the products. How can I do something like this say, I need only the products which has key="content" and value="chicken".

@Zhuinden
Copy link
Contributor

Zhuinden commented Jul 19, 2017

realm.where(Product.class)
         .equalTo("meta.key","content")
         .findAll()
         .where()
         .equalTo("meta.value","chicken")
         .findAll()

@pollux-
Copy link
Author

pollux- commented Jul 19, 2017

@Zhuinden thanks , Awesome, will try this

@cmelchior
Copy link
Contributor

@pollux- You can also read this relevant section in the docs: https://realm.io/docs/java/latest/#link-queries

@pollux-
Copy link
Author

pollux- commented Jul 19, 2017

Awesome, working as expected 👍

       
 final Realm dbInstance = DbManager.getInstance().getDBInstance();
        dbInstance.executeTransaction(new Realm.Transaction() {
            @Override
            public void execute(Realm realm) {

                try {
                    RealmResults<TestProduct> product = dbInstance.where(TestProduct.class).findAll();
                    if (product.size() ==0) {
                        final InputStream testJson = getAssets().open("test_json");
                        dbInstance.createAllFromJson(TestProduct.class, testJson);
                    }
                    RealmResults<TestProduct> all = dbInstance.where(TestProduct.class).equalTo("meta.key","content").findAll().where().equalTo("meta.value","chicken").findAll();
                    int size = all.size();

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        });

@pollux- pollux- closed this as completed Jul 19, 2017
@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