I'm starting to use Realm to store objects in my Android app. Here's an example of what I'm looking to store:
public class Item implements RealmModel {
String id;
...
}
I have multiple lists that display items. The number of lists can keep expanding (the user can create as many lists as possible.
Let's say the user create's a list called "Best". When I'm viewing the "Best" list I call getItems("Best")
which gets a List<Items>
from the API. I now have to figure out how to store this list. In SQLite world I would have create a new table "custom_list_best", for example, which is just a single list of all the Item.id's that are part of the list. I would also have an "items" table that has all the distinct Items. To get items in the best list I would simply do a join query on both the best and items tables.
In the Realm world I'm trying to figure out how Realm works and what's the best way to build my models.
I initially thought I could create an object called CustomList
:
public class CustomList implements RealmModel {
String listId;
RealmList<Item> items;
}
I would then store a RealmList<CustomList>
. But the only issue is I also want to be able to query against all Items. So I need to also store a RealmList<Item>
in Realm. How does Realm work in this case? If I store a separate RealmList<Item>
and then store each RealmList<CustomList>
will it not duplicate data?
Instead will I have to manually handle this by doing this instead:
public class CustomList implements RealmModel {
String listId;
List<String> itemIds;
}
And then query for Item.class
objects that have itemId in itemIds from above object?
No, you would have a table called
custom_lists
with an auto-increment ID and an identifier, and a join table calledjoin_custom_lists_items
that would contain the ID ofcustom_lists
and the ID of anyitem
that belongs to that given custom list.If the
ID
of the item is concrete and you need to be able to store the sameItem
in multiple lists, then in order to access the lists in both directions, you'll need aRealmList<? extends RealmModel>
in both cases.And
This way you can do
And then you can query
All this
Fields
stuff I'm using is from https://github.com/cmelchior/realmfieldnameshelper