Reputation: 1253
I have 2 classes: Posts class and User class. I have been trying to query Posts class to show only posts from current user's following. I do it like this:
[query whereKey:@"user" containedIn:[PFUser currentUser][@"followings"]];
Column "followings" is array containing objectId of current user's following. My problem seems to lie where the key @"user" in Posts class is pointer to User class and causes this error:
Error: pointer field user needs a pointer value (Code: 102, Version: 1.2.10)
How should I go about this? Thanks in advance.
Edit1: Info added
Here I provide details about the mentioned 2 classes:
objectId(string)----text(string)----user(pointer)----...
objectId(string)----username(string)----followings(array of users'objectId)----...
Upvotes: 1
Views: 2555
Reputation: 17695
There is the swift version for the checked response
//Create an user query
let userQuery = PFUser.query()
//get all PFUser contained in string array
userQuery?.whereKey("objectId", containedIn: PFUser.currentUser()!["FollowArray"]as! [String])
//add subquery
query.whereKey("user", matchesQuery: userQuery!)
Upvotes: 1
Reputation: 496
To get the posts only of current user you need to user Where query from parse in which you need to pass the current user
I am posting here a small code snippet that can give you clear idea
PFQuery *query = [PFQuery queryWithClassName:@"PostTable"];
[query whereKey:@"user" equalTo:[PFUser currentUser]]; // "user" must be pointer in the post class (table)
[query orderByDescending:@"createdAt"];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
for (PFObject *obj in objects) {
NSLog(@"object %@",obj);
}
}
if (error) {
[_delegate gotError:error forTable:TABLE_MEDIA];
}
}];
Here in above Code snippet it will only give you the post belonging to your current user, If you want the post of any other user you should past that users instance in where query.
So to have the posts of following user you must get that all following users from parse with pointers and have above query to gets their posts.
To have the pointers in an single row in Array refer below code.
[userClass addObject:userPointer forKey:@"following"];
So now when you get the user you will have all the following users with pointer and you can get its post by above code snippet
Upvotes: 0
Reputation: 1253
I have achieved querying only posts from current user's followings by using matchesQuery:
PFQuery *userQuery = [PFUser query];
[userQuery whereKey:@"objectId" containedIn:[PFUser currentUser][@"followings"]];
[query whereKey:@"user" matchesQuery:userQuery];
Upvotes: 8
Reputation: 865
As far as I know you can't have pointers in an array in Parse, you would have to put the user's objectId in the array "followings" and then search for that value in the array. then you can search for the PFUser like this:
PFQuery *query = [PFUser query];
[query whereKey:@"objectId" equalTo:@"INSERT-OBJECT-ID-IN-ARRAY"];
PFUser *user = (PFUser *)[query getFirstObject];
Upvotes: 0