Skip to content

Parse Supports Many to Many Relations

There is updated content on this subject. Please see this post for more information.

A few days ago, we came out of beta with our 1.0 SDK. The 1.0 blog post discusses a few backwards incompatible changes, but I'm here to talk about some of the awesome new features that it included. In the new SDK, Parse supports a richer relational model by allowing many to many relations.

Let's say you are writing a hot new social app that records which people like which local establishments. You can store an array of users who like a particular place by doing:

 // Let's say people is a NSArray of "Person" PFObjects.
 PFObject *place = [PFObject objectWithClassName:@"Place];
 [place setObject:@"Sid's BBQ" forKey:@"name"];
 [place setObject:people forKey:@"likes"];
 [place saveInBackground];

You can then search for places that Bob liked by doing:

  PFObject *personNamedBob = ...;
  PFQuery *query = [PFQuery queryWithClassName:@"Place"];
  [query whereKey:@"likes" equalTo:personNamedBob];

You can also do more complicated queries such as finding all places liked by people in Australia. It's as simple as:

// Create a query for People in Australia
PFQuery *query1 = [PFQuery queryWithClassName:@"Person"];
[query1 whereKey:@"location" equalTo:@"Australia"];

// Create a query for Places liked by People in Australia.
PFQuery *query2 = [PFQuery queryWithClassName:@"Place"];
[query2 whereKey:@"likes" matchesQuery:query1];
[query2 findObjectsInBackgroundWithBlock:^(NSArray *results, NSError*error) {
   // results contains the places that are liked by people from Australia

These new features make storing and retrieving your data from Parse even easier. And of course, if you have any feedback, let us know.