iOS-解析框架嵌套查询

时间:2015-08-14 06:31:33

标签: ios objective-c iphone parse-platform

我有两张表TrendingUsersFollow。所需的功能就像从TrendingUsers表中获取用户并提供跟随,前提是获取的用户不是来自用户的关注列表。如果用户已经被跟踪,则跳过。

Follow表格包含followerleader列。

PFQuery *followTableQuery = [PFQuery queryWithClassName:@"Follow"];
[followTableQuery whereKey:@"follower" equalTo:[PFUser currentUser] ];
[followTableQuery whereKey:@"leader" equalTo:@"fetchedUserObject" ];
[followTableQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
    if (!error) {
        if (objects.count) {
          //if following objects array will have single object
        }
        else
        {
            //not following to @"fetchedUserObject" user
        }

    }
  }
 ];

这将确认currentUser是否跟随@"fetchedUserObject"用户。 现在我想将此集成到TrendingUsers表查询中,以仅获取currentUser未跟踪的用户。

1 个答案:

答案 0 :(得分:2)

您可以简单地使用嵌套查询,来自Parse的文档通常是一个很好的起点。这是一个示例代码,根据我从您的问题中理解,这应该可以解决问题。

//This is our current user
PFUser *user = [PFUser currentUser];

//The first query, querying for all the follow objects from the current user
PFQuery *followingQuery = [PFQuery queryWithClassName:@"Follow"];
[followingQuery whereKey:@"follower" equalTo:user];

//Now we query for the actual trending users, but we do not want the query to return the users (who are in the @"leader" key) that have been found by the first query
PFQuery *trendingQuery = [PFQuery queryWithClassName:@"TrendingUsers"];
[trendingQuery whereKey:@"objectId" notEqualTo:user.objectId]; //don't return the current user
[trendingQuery whereKey:@"objectId" doesNotMatchKey:@"leader" inQuery:followingQuery]; //I'm supposing that @"leader" is containing the objectId of the specific user that is part of the follow object with the current user
[trendingQuery setLimit:1000];
[trendingQuery findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error)
{
    //...
}];

我可能没有完全理解您的数据结构,因此您可能需要在上面的代码中交换一个或多个键,但基本上,这就是您要这样做的方法。