0

I'm new to Parse.com and was having trouble designing the structure of my database, and how to retrieve the desired data.

In the database, each user (primary identifier as email) has a list of friends and a status boolean. The friend list contains the email of other users in the database. I need to get the status boolean for each of the friends in a particular users list, and preferably in a single query to the server.

What would be a good way to design our structure and retrieve this data. Currently, I made two data classes (tables), one containing each user with their boolean status, and another containing each user and their list of friends. Firstly I was not sure if this structure is the correct way to go. Secondly, I don't know how to retrieve the status boolean for each user in a single users friend list.

Edit I actually discovered the relation column type just yesterday, but I was unable to figure out how to use it. 1) How do I link a Persona to a User in code? I understand I need to use ObjectID here, but how?
2) How do I add other Personae (friends) to a relation of a single Persona (the user). I was unable to populate this relation column. I understand query can be used on the relation column, but I couldn't reach that far ahead without populating the relation column.
3) In my query to the server, am I pulling the entire table? Lets say a user has 2 friends. Is there a way for me to fetch only the current user, and the two friends, or am I pulling the entire table, and then doing my filtering on it. I am concerned with the network being burdened if my table of users grows big.

Edit Well I couldn't figure out relational queries perfectly just yet, however, I found a good solution to my problem. Since the list of friends changes very rarely, I'll be maintaining this list offline, resulting in a single query to the server of pulling in the status of my friends. Along with this list, I may or may not also decide to pull in my own data and get an updated friend list. Thank you for your help though.

2
  • and another containing each user and their list of friends don't store lists as columns. You need a third table to associate users to friends. Commented Mar 7, 2015 at 0:01
  • @Falmarri how do I do that association? That is what I am trying to achieve by storing a list of users (friends) for each user. Commented Mar 7, 2015 at 0:10

1 Answer 1

1

The way to model many-to-many relations in parse is with the relation column type. This is the best choice to describe how a user has many friends who are users. If this is a social-network-like app, another good bit of advice is to create a class -- distinct from the parse User -- that describes users' public personae.

This is so you can have the parse User class remain as the private, customer relationship between your app and a real person (there are built in security constraints here). This other table, say we call it Persona, can have a pointer-typed column to its user, keep such things as nickname, profile image, etc. and also keep your boolean status.

_User class - default stuff that comes standard with parse, plus anything pertaining to the customer relationship with your app.

Persona - pointer to _User table, boolean status, other public info, relation called "friends" relating this to other Persona.

So, given a logged in user and his/her currently selected persona (your choice whether users may have more than one personae), you can get friends' personae as follows (in pseudo code):

friendsRelation <- myPersona.friends
friendsQuery <- friendsRelation.query  // query is a method on relation
run friendsQuery asynch, then the result will be allFriendsPersonae
    for each persona in allFriendsPersonae
        status <- persona.status

If you choose not to take the persona class advice, the "code" above is the same, just replace persona with user.

Edit - in response to question edit: 1) Link a persona the user by setting the persona's user column (pointer type) to the user object. To get that persona later, when you only have a user, query the persona table where "user" column equals user.

2) Relation implements an add() method. If you have a personaA, and want to add personaB as a friend, you getRelation("friends") on personaA, and send it add(personaB).

3) The query you get from a relation is a query only for members of that relation. So if personaA has two friends personaB and personaC, you'll get only B and C when you run personaA's friends query.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks danH. I've added an 'edit' in my question in response to this.
1) Is this what youre saying: persona.put("user", ParseObject.createWithoutData("User", "user_object_id_here")); 2) I was trying that out, but couldnt really get it to work. I'll try this again today. 3) So there will be 2 queries to the server? Once on personaA, and then a query for personaB, personaC? Or will there be 3 queries one for each persona?
1) yes, but you'll probably have the user object at that point, not just the id, so you can .put("user", user). 2) remember to save, 3) counting starting with just the current user, then you'll do two queries: the first one to find personaA (find Persona where user equals current user), and one to run personaA's friends query which will return two results (B and C).
I dont understand the point of using pointers (no pun intended). For PersonA, I could simply have an array of email addresses for the friends. So one query would fetch this array, and query would return all users with the email addresses in that array. Why use objectID/pointers then?
Two decisions: array vs relation, and (if array) pointers vs. some other identifying attribute. Array is acceptable (even preferable) for small one-to-many associations, but relations required for large and many-to-many. About what to put in the array, pointers are better because you can eagerly fetch the related objects on a query using include.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.