0

いくつかの複雑なクエリがあり、PFRelation で問題が発生している iOS アプリを Parse で構築しています。

これは、人々が記事を投稿するソーシャル ネットワーク Web サイトです。他のユーザーをフォローして、投稿された記事を見ることができます。トピックに基づいて検索することもできます。

これは私が持っているコードです

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 に答える