Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Fabio Floris
Fabio Floris
526 Points

Parse.com Query for PFUser CurrentUser

Hello to everyone in the query I posted I'm trying to retrieve all of the posts and PfUserCurrent all posts of people who have shared a friendship with PfUserCurrent.

The query works fine but I have only one problem, the query shows me all the posts of Friends of the CurrentUser but does not show me those sent by the CurrentUser ... I've tried several attempts but I could not fix this ... Can you explain where I'm wrong?

-(void)QueryForPost {

    PFQuery *QueryForFriend=[PFQuery queryWithClassName:@"Amicizie"];
    [QueryForFriend whereKey:@"A_User" equalTo:[PFUser currentUser]];
    [QueryForFriend whereKey:@"STATO"  equalTo:@"Confermato"];



    PFQuery *QueryYES = [PFQuery queryWithClassName:@"Post"];
    [QueryYES whereKey:@"FLASH_POST" equalTo:[NSNumber numberWithBool:YES]];
    [QueryYES whereKey:@"Scelti" equalTo:[PFUser currentUser]];


    PFQuery *QueryNO = [PFQuery queryWithClassName:@"Post"];
    [QueryNO whereKey:@"FLASH_POST" equalTo:[NSNumber numberWithBool:NO]];
    [QueryNO whereKey:@"Utente" matchesKey:@"Da_User" inQuery:QueryForFriend];

    PFQuery *query = [PFQuery orQueryWithSubqueries:@[QueryYES,QueryNO]];
    [query includeKey:@"Utente"];

    [query orderByDescending:FF_CREATEDAT];
    [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
        if (!error) {
            NSLog(@"%@", results);
            ArrayforPost = [[NSMutableArray alloc] init];
            for (PFObject *object in results) {
                [ArrayforPost addObject:object];
            }
            [self.FFTableView reloadData];
        }
    }];



}

2 Answers

Fabio Floris
Fabio Floris
526 Points

Hello Ben! Thank you very much for the help ... I solved in this way and it seems to work perfectly: D

        PFQuery *QueryForFriend=[PFQuery queryWithClassName:FF_AMICIZIE_CLASS];
        [QueryForFriend whereKey:FF_AMICIZIE_A_USER equalTo:[PFUser currentUser]];
        [QueryForFriend whereKey:FF_AMICIZIE_STATO  equalTo:@"Confermato"];
        [QueryForFriend includeKey:FF_AMICIZIE_DA_USER];

        PFQuery *QueryYES = [PFQuery queryWithClassName:FF_POST_CLASS];
        [QueryYES whereKey:FF_POST_FLASH_POST_BOOLEANVALUE equalTo:[NSNumber numberWithBool:YES]];
        [QueryYES whereKey:FF_POST_SCELTI equalTo:[PFUser currentUser]];

        PFQuery *normalPostByFriends = [PFQuery queryWithClassName: FF_POST_CLASS];
        [normalPostByFriends whereKey: FF_POST_FLASH_POST_BOOLEANVALUE  equalTo: [NSNumber numberWithBool: NO]];
        [normalPostByFriends whereKey: FF_POST_UTENTE matchesKey:FF_AMICIZIE_DA_USER inQuery:QueryForFriend];

        PFQuery *normalPostByUser = [PFQuery queryWithClassName:FF_POST_CLASS];
        [normalPostByUser whereKey:FF_POST_FLASH_POST_BOOLEANVALUE  equalTo: [NSNumber numberWithBool: NO]];
        [normalPostByUser whereKey:FF_POST_UTENTE equalTo: [PFUser currentUser]];

        PFQuery *query = [PFQuery orQueryWithSubqueries:@[QueryYES,normalPostByFriends,normalPostByUser]];

            [query includeKey:FF_POST_UTENTE];


            [query orderByDescending:FF_CREATEDAT];

            [query findObjectsInBackgroundWithBlock:^(NSArray *results, NSError *error) {
                if (!error) {
                    NSLog(@"%@", results);
                    ArrayforPost = [[NSMutableArray alloc] init];
                    for (PFObject *object in results) {
                        [ArrayforPost addObject:object];
                    }
                    [self.FFTableView reloadData];
                }
            }];
Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Might an additional subquery to get the posts for the current user in addition to those returned by QueryYes and QueryNo work?