anotherDeveloper
anotherDeveloper

Reputation: 77

Parse Pointers and Relations

So right now I have two classes. A User class, and a UserUpdates class. the UserUpdates class is used to handle friendRequests, and friends, and what not. I have a field in my UserUpdates class called friendsArray, I want to have a field in my User class called friendsArray as well, that will just point to the UserUpdates friendsArray so I dont have to do a seperate query.

So in summary, what I want to accomplish is have a field in my User class, that updates automatically or points to the friendsArray in the other class (UserUpdates)

The friendsArray is simply an array of strings which are usernames

How can I accomplish this using parse's api?

Upvotes: 1

Views: 1116

Answers (2)

Marius Waldal
Marius Waldal

Reputation: 9942

It is not possible to point a property of another object; you can only point to the object containing that property (the friendsArray).

If what you're trying to accomplish is to easily get the friendsArray from the UserUpdates class, you can do that like this, if the User class has a pointer to the UserUpdates object:

PFQuery *userQuery = [PFUser query];
// add constraints to get the correct user
[userQuery includeKey:@"UserUpdates"]; // ensures the UserUpdates object is downloaded with the User object

NSArray *results = [userQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        PFUser *theUser = [objects lastObject]; // If the constraints returns only 1 object
        PFObject *userUpdates = theUser[@"UserUpdates"];
        NSArray *friends = userUpdates[@"friendsArray"]; // Now you have the friends array
    }];

It might be possible to chain the last calls together like this, but I am unsure. Try it out:

NSArray *friends = theUser[@"UserUpdates"][@"friendsArray"];

Upvotes: 1

Saoud Rizwan
Saoud Rizwan

Reputation: 659

Simply put: you cannot.

But I get what you're trying to accomplish, and that method is not effective, it's just better to have one class that has multiple fields for each user.

I created a simple friending system with Parse that got way too complicated way too quickly and I suggest you take caution in friending system leaks and to keep everything as simple as possible.

Upvotes: 0

Related Questions