简体   繁体   中英

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 ?

EDIT

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 ##

  • name
  • age

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.

Yes, you can query all 3 classes using 1 class. I would create a relationships class, which has pointers to each of the 3 classes you want to query, and then directly query this relationships class to include the pointers to the 3 classes. Then you can just create objects of the 3 classes and use them.

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.

Yes and no. If you are retrieving from separate classes, you need to do 3 separate callbacks. But depending on how your structured and what your really trying to accomplish, you could look into Relational queries

you can add as a relation your custom class instead of _User

In your custom class you can store a user as a one field and his status (lover, eater, maker) on the other so you can query for all users at once

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM