Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Query with pointer User in Parse.com

I am using Parse.com in my app, I have created a "Post" class and inside I have a pointer that specifies which user sent the post. Now I seeing on the timeline to the Post and the time the user name who created the post. Since the user is a pointer I can not make a correct query, do you know how can I fix this issue?

like image 304
kAiN Avatar asked Dec 21 '25 05:12

kAiN


2 Answers

The trick is to use PFQuery includeKey to have the related pointer data automatically filled by Parse. The user data will be downloaded without requiring you to make another query for it.

PFQuery *query = [PFQuery queryWithClassName:@"Post"];

// Include the user data with each post
[query includeKey:@"title_of_userpointer_column"];

[query findObjectsInBackgroundWithBlock:^(NSArray *postObjects, NSError *error)
 {
     if (!error) {
         NSLog(@"Successfully retrieved: %d items", [postObjects count]);

         // postObjects now contains the latest 100 Posts, and the "title_of_userpointer_column" field
         // has been populated.
         // For example:
         for (PFObject * postObject in postObjects) {

             // This does not require a network access.
             PFObject *postAuthor = [postObject objectForKey:@"title_of_userpointer_column"];
             NSLog(@"retrieved related Post Author: %@", postAuthor);
         }
 }
like image 150
Phil Wilson Avatar answered Dec 24 '25 09:12

Phil Wilson


You can retrieve the pointer to User field from the Post table by adding this line of code (in Android):

postQuery.include("pointerToUser");

postQuery is query of class Post. By this way, after executing the query the object or objects that will be returned would have the user information.

You can see a full example here: How to query value of column that is set as pointer to other table in Parse

like image 43
vlio20 Avatar answered Dec 24 '25 09:12

vlio20



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!