Skip to content Skip to sidebar Skip to footer

Parse Relational Data Queries

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) h

Solution 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.

Post a Comment for "Parse Relational Data Queries"