Bernard
Bernard

Reputation: 4580

ios parse _user pointer equal to user id?

I have a table with a column which is pinter to _User. I want to check one string is equal to the user id I have in my table.

enter image description here

In this example I want to check if iSMSxjAnvJ is equal to user column. Here is my code:

PFQuery *query2 =  [PFQuery queryWithClassName:@"Friends"];
[query2 whereKey:@"user" equalTo:self.friendID];

[query2 findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
           for (PFObject *friendRecord in objects) {
                        // do something with a single example object here.
           }
}];

But I get this error:

[Error]: pointer field user needs a pointer value (Code: 102, Version: 1.7.5)

I need to change this pointer value to string type to do comparison. One way is to query my user table and get string value. Is there any better way?

Upvotes: 3

Views: 389

Answers (1)

danh
danh

Reputation: 62676

The operand of equalTo: for a pointer column is a PFObject, either a complete object or an empty PFObject with just an objectId (a "pointer" in parse world). In other words, say you have a a real PFUser object...

PFUser *friend = // imagine you have this somehow.

To test whether a Friend instance's user is friend, just say...

PFQuery *query2 =  [PFQuery queryWithClassName:@"Friends"];
[query2 whereKey:@"user" equalTo:friend];

But what if you don't have a object? Like the name friendId implies, what if you only have an objectId string? Two answers:

Short answer: Make the objectId into a pointer...

PFUser *friendPointer = [PFUser objectWithoutDataWithClassName:@"_User" objectId:self.friendID];
PFQuery *query2 =  [PFQuery queryWithClassName:@"Friends"];
[query2 whereKey:@"user" equalTo:friendPointer];

Long answer: starts with a question: why do you have only the object id, why don't you have a real object? Change the friendId (string) column on self to a friend column (pointer to _User). Then...

// put a column called friend on self. Make it a pointer to _User
PFUser *friend = self.friend;  // we imagined this before, now its real!
PFQuery *query2 =  [PFQuery queryWithClassName:@"Friends"];
[query2 whereKey:@"user" equalTo:friend];

Upvotes: 4

Related Questions