简体   繁体   中英

Parse PFRelation complex search

I'm building an iOS app with Parse that has a few complex queries and running into some trouble with PFRelation.

It's a social network website where people submit articles. You can follow other users and see the articles they submitted. You can also search based on topics.

This is the code I have

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

}];
}

Thank you for your time reading this.

I was able to resolve this issue by creating a subquery:

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
           …...
    }];

}];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM