Question

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:@"Friendships"];
    [QueryForFriend whereKey:@"To_User" equalTo:[PFUser currentUser]];
    [QueryForFriend whereKey:@"STATUS"  equalTo:@"Confirmed"];



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


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

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

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



}
Was it helpful?

Solution

After a long discussion with @rory, here we come to the correct answer:

-(void)QueryForPost {

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

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

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

PFQuery *normalPostByUser = [PFQuery queryWithClassName:@"Post"]; 
[normalPostByUser whereKey: @"FLASH_POST" equalTo: [NSNumber numberWithBool: NO]]; 
[normalPostByUser whereKey: @"Utente" equalTo: [PFUser currentUser]];

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

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

}

The problem is handled by making another query to display current user's normal post and modify QueryForFriend for a correct query

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top