Android : Get objects from multiple Relations
Say, I have a Food class, in which I have 3 relations - 'Lovers', 'Eaters', 'Makers'. I added users to each of these relations.
Now I want to display for a particular food, a list which shows all 'Lovers' first, then 'Makers', and all 'Eaters' at the end - in this order.
So to retrieve only Lovers, I have to do ParseQuery on Lovers, and then find(), from which I store the list of users in an ArrayList. Then again I repeat the same procedure for Makers and Eaters and append the result to my ArrayList.
This process is slow, tedious and resource consuming. Is there a better way ?
Food class(ParseObject) is as follows
- Lovers - Relation<_User>
- Eaters - Relation<_User>
- Makers - Relation<_User>
- --- and other variables like cuisine, country, etc ---
User class(ParseUser) is as follows ##
Every food makes a 'Lovers' relation with users who love the food, same for 'Eaters' and 'Makers'
To get Lovers, then Makers and then Eaters for a 'food' object, I use following query
ArrayList<User> userList = new ArrayList<User>(); ParseRelation<ParseObject> relation = food.getRelation("Lovers"); ArrayList<User> lovers =relation.getQuery().find(); ParseRelation<ParseObject> relation = food.getRelation("Makers"); ArrayList<User> makers =relation.getQuery().find(); ParseRelation<ParseObject> relation = food.getRelation("Eaters"); ArrayList<User> eaters =relation.getQuery().find(); userList.add(lovers); userList.add(makers); userList.add(eaters);
Unfortunately Parse does not support 'include' in query on relations and I dont want to use Array to store the relationship (coz in future I may need to find what a user loves, eats and makes). So, is there a solution get these 3 relations data in single query.
One option would be to create a class to be your join table, as is done in some of the samples provided on Parse.
UserFood: User: link to User class Food: link to Food class MakesIt: bool LovesIt: bool EatsIt: bool
Getting all users for a food becomes a simple query, getting all foods for a user becomes simple. If you just want a list of 'Lovers' you can filter on the 'LovesIt' = true.
It also makes it easy to do a query like: find my all foods where the person that makes it also eats it.