Apollo
Apollo

Reputation: 9054

ios parse one query many to many relationship

I have the following table structure:

User

id
email
etc

Conversation

id

Message

Id
Convo Id
User Id (the creator)
data

Participants

Convo_id 
User_id (the user who is part of the conversation)

My question is using Parse.com how do I formulate a query so that I can do the following:

As user a, I want to get all of my conversations. Therefore first go through the Participants table and get each Convo_id where User_id = UserA.id

Then I have an array of Convo_id's that UserA is having. My problem is how do I go back and get the other participants using each Convo_id from the original query?

Upvotes: 0

Views: 230

Answers (1)

Timothy Walters
Timothy Walters

Reputation: 16874

From the looks of your question, the Participants class should actually just be an array of User on the Conversation.

If your model is built as I describe, then you can just query Conversation as follows:

query.equalTo('Participants', userA);

Using equalTo() on an array property means "array contains this value". When the value is a ParseObject it will compare the objectId and see if that is in the array.

Also, make sure you don't manually store the ID, in code you should store the Parse Object, it will then store the ID as a reference that means you can use the system better.

In the case that you actually do need to use a Participants class as a many-to-many join, you can use:

query.equalTo("user", userA);
// include the full conversation class from its link
query.include("convo");

The results will then be Participants with a convo property that is a full Conversation instance. In theory you could end up with the same conversation more than once if your data allows, it is up to you to filter out duplicates if needed.

Upvotes: 1

Related Questions