3

Reference to realms doc

I have some entities, for example it is: Category and Item.
The Category contains RealmList of Items and I can access all items of category by calling getter of this list.
But how can I query all items by category's id(it's annotated as primary key)
I'm parsing json via Realm.createObjectFromJson() and can't set Category field to each Item
Thanx in advance

uneven
  • 341
  • 2
  • 13

1 Answers1

6

If there isn't any link from your Item to your Category you cannot currently query Items based on the Category. The concept you are looking for is on our TODO and is called backlinks. You can follow progress on that here: https://github.com/realm/realm-java/issues/607

The current workaround would be to manually create that link after you copied them to Realm:

realm.beginTransaction();
Category category = realm.createObjectFromJson(categoryJson);
for (Item item : category.getItems()) {
  item.setCategory(category);
}
realm.commitTransaction();

// Then you can do
realm.where(Item.class).equalTo("category.id", category.getId()).findAll();
Christian Melchior
  • 19,978
  • 5
  • 62
  • 53