Query with pointer User in Parse.com

江枫思渺然 提交于 2019-12-12 14:15:54

问题


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?


回答1:


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);
         }
 }



回答2:


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



来源:https://stackoverflow.com/questions/19099137/query-with-pointer-user-in-parse-com

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!