0

我正在使用 Parse 构建一个 iOS 应用程序,该应用程序有一些复杂的查询,并且在使用 PFRelation 时遇到了一些问题。

这是一个人们提交文章的社交网络网站。您可以关注其他用户并查看他们提交的文章。您还可以根据主题进行搜索。

这是我的代码

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

//remove articles this user has seen or submitted
[query whereKey:@"likedBy" notEqualTo:currentUser]; //'likedBy' is a relation
[query whereKey:@"dislikedBy" notEqualTo:currentUser]; //'dislikedBy' is a relation
[query whereKey:@"submittedBy" notEqualTo:currentUser]; //'submittedBy' is a relation

[query whereKey:@"tagArray" containedIn:tags];
[query orderByDescending:@"createdAt"];

PFRelation* relation = [currentUser relationForKey:@"following"]; //following is a relation for the user
PFQuery* followingQuery = [relation query];
[followingQuery findObjectsInBackgroundWithBlock:^(NSArray* results, NSError* error) {

    //results from this first query is the list of people the user is following

    [query whereKey:@"submittedBy" containedIn:results]; //submitted by is a relation on the article
    [query findObjectsInBackgroundWithBlock:^(NSArray* results, NSError* error) {

        /* This will return all the items that match the tags set above.
        However, if there are no tags, I do not get the articles
        that match the "submittedBy" above. It is empty */
        completion(results);
    }];

}];
}

感谢您花时间阅读本文。

4

1 回答 1

0

我能够通过创建一个子查询来解决这个问题:

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

//remove articles this user has seen or submitted
[query whereKey:@"likedBy" notEqualTo:currentUser]; //'likedBy' is a relation
[query whereKey:@"dislikedBy" notEqualTo:currentUser]; //'dislikedBy' is a relation
[query whereKey:@"submittedBy" notEqualTo:currentUser]; //'submittedBy' is a relation

[query whereKey:@"tagArray" containedIn:tags];

// create an array to hold all of the queries
NSMutableArray* queryArray = [NSMutableArray arrayWithCapacity:10];

PFRelation* relation = [currentUser relationForKey:@"following"]; //following is a relation for the user
PFQuery* followingQuery = [relation query];
[followingQuery findObjectsInBackgroundWithBlock:^(NSArray* results, NSError* error) {

    //results from this first query is the list of people the user is following
   for (PFObject* user in results) {
            //create a new query for each result and add it to the query array
                    PFQuery* querySub = [PFQuery queryWithClassName:@"App"];
                     [querySub whereKey:@"submittedBy" equalTo:user];
                     [queryArray addObject:querySub];
                }

                //query all of the queries at once
                PFQuery* finalQuery = [PFQuery orQueryWithSubqueries:queryArray];

    [finalQuery findObjectsInBackgroundWithBlock:^(NSArray* results, NSError* error) {
    //NOTE: you cannot sort via query with subqueries, so I had to sort the results array when completed
           …...
    }];

}];
}
于 2016-01-21T13:00:15.733 回答